Need to include | after every field - unix

March 30, 2011 at 08:30:07
Specs: AIX
Hi all,

I have a "|" delimited file with almost 80 fields in every record , I'm reading the records and reformatting some fields. The question I have is when I print the output record how can I include the "|" after every field?

Thank you

Thank you

See More: Need to include | after every field - unix

Report •

March 30, 2011 at 09:10:24
You don't say what tool you are using to reformat the fields. I am assuming you are using the shell. I can see where it would be tedious to print all 80 fields out individually with the field seperator pipe symbol.

Perhaps you should try another tool like perl or awk. awk let's you control the output field seperator, OFS, as well as the input field seperator, FS. Here is an example:

Report •

March 30, 2011 at 10:11:43
Sorry, forgot to mention. I'm using ksh, the script is working but the "|" is not printing. Here's what I have so far

while read a1 a2 a3 a4 a5 ….. ……..
yy=`expr substr "$a3" 7 2`
print $acyy $a2 $a3
done < acctin.txt > acctout.txt

Report •

March 30, 2011 at 11:49:25
The way you are using the print command, the shell does not automatically print a field seperator. You have to explicitly print it:


print ${acyy}${s}${a2}${s}${a3}

You could use the printf command and embed the field seperator in the format:

printf "%s|%s|%s\n" $acyy, $a2, $a3

Either way, having to print out 80 fields and the seperator would not be any fun. That is why I suggested awk:

awk ' BEGIN { FS=OFS="|" }
   print $0
} ' acctin.txt > acctout.txt

# end script

awk does the parsing for you. $1 is field 1, $3 is field 3, $0 is the entire line. You can control the field seperators using FS and OFS.

Report •

Related Solutions

March 31, 2011 at 04:23:22
That worked, many thanks

Report •

Ask Question