unix script to copy each row line from file t

November 19, 2010 at 03:41:56
Specs: Windows XP
i need to write script to copy the txt file to excel. (data can be 2000+), data may not be in order

ex:

my name: abc
age: 20
DOB: 17-mar-2010
add: xyz

my name: cat
age: 35
DOB: 11-oct-2005
city: yeshjl
add: opq

my name: def
age: 21
DOB: 17-apr-2010
add: uvw

require format as below in excel, columnwise

abc 20 17-mar-2010 xyz
cat 35 11-oct-2005 opq yeshjl
def 21 17-apr-2010 uvw


See More: unix script to copy each row line from file t

Report •

#1
November 19, 2010 at 09:12:01
Set the field seperator to a colon so each of the records contains two fields. skip the blank lines. Print a space after the field except the add field print a line feed:

#!/bin/ksh

# set field seperator, IFS, to a colon, read
# each line and divide it into two columns
while IFS=":" read col1 col2
do
   # skip the blank line
   if [[ $col1 == "" ]]
   then
      continue
   fi
   # if the first column equals string "add" print
   # the second column with a carriage 
   # return, CR
   if [[ "$col1" == "add" ]]
   then
      printf "%s\n" "$col2"
   else
      # or print the second column with a space
      # after it (No carriage return
      printf "%s " "$col2"
   fi
done < datafile.txt

and, of course, there is always awk:

#!/bin/ksh

# with awk, the field seperator is FS.  set it
# at the BEGINning of the script
awk ' BEGIN {  FS=":" }
{
# in awk, NF is the number of fields in the
# input line
   if(NF == 2)
      if($1 == "add") # if first field equals "add"
                            # print the second field with
                            # CR
         printf("%s\n", $2)
      else
         printf("%s", $2) # if not "add", print with
                                # no CR
} '  datafile.txt



Report •

#2
November 21, 2010 at 22:50:08
thx nails,
but i want to view the output in excel..
wen i see the above o/p in excel , it is in the same line in 1 column,,, i need the o/p column wise... filed separeted ny column

Report •

#3
November 22, 2010 at 08:39:52
Sorry, but I'm not an excel guy. I thought you wanted to manipulate a text file and import it into excel.

Report •

Related Solutions

#4
November 23, 2010 at 22:20:48
can u please help me with my query ,

Report •

#5
November 27, 2010 at 13:48:24
I have rewritten the korn shell script so a carriage return is printed when the first field equals "my name" except the first instance:

#!/bin/ksh

first_read=0
while IFS=":" read col1 col2
do
   # skip the blank line
   if [[ $col1 == "" ]]
   then
      continue
   fi

   if [[ "$col1" == "my name" && $first_read -eq 0 ]]
   then
      printf "%s " $col2
      first_read=1
      continue
   fi

   # print carriage return if there is a new header
   if [[ "$col1" == "my name" ]]
   then
      printf "\n%s " $col2
   else
      printf "%s " $col2
   fi
done < datafile.txt


Report •

#6
November 30, 2010 at 21:38:28
what exactly i want here is, it should search for particular string like, "my name" or "add" and then place its value columnwise... as i said i have lots of data and it differ with its content.. and i want the data only for some of the variables... can u help me with it... ur coding wil make me learn lots...

Report •

Ask Question