Home » Infrastructure » Unix » Unix Shell.
Unix Shell. [message #98399] Sun, 12 September 2004 22:21 Go to next message
Vidyanand More
Messages: 35
Registered: January 2003
Member
Hi All,
I need to following in Unix Text File.

Let us consider following is a text file.

chk13092004.txt
Header^M
dasfs fdsfdf fgfdgfd^M
fdsfsdf gdfgfg dsgdfg^M
fsdfdsfdsf fdgdfg^M
fdggf^M
...
...
Trailer

1> Remove Header & Tailer from the text file.
2> Replace occurence of '^M' with ''
3> Add Filename as last column in all the records.

Can some one please help me?

Thanks in Advance.
Regards,
Vidyanand
Re: Unix Shell. [message #98402 is a reply to message #98399] Mon, 13 September 2004 13:02 Go to previous message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
on Solaris, you can try "/usr/bin/dos2unix in.txt out.txt" to fix the CR-LF. You could also try "cat in.txt | tr '13' '' > out.txt"

If Header and Trailer are fixed strings which don't occur in the data, you can try:
"grep -v Header in.txt | grep -v Trailer > out.txt"

this example trims top and bottom off of file:
#!/bin/ksh
export TRIM_HEAD=5
export TRIM_TAIL=3
export LINES=`wc -l zz.txt | awk '{ print $1 }'`
echo Lines in zz.txt=$LINES

nawk -F "&#124" '{if(NR > ENVIRON[["TRIM_HEAD"]] && NR < ENVIRON[["LINES"]]-ENVIRON[["TRIM_TAIL"]]) print $0 }' in.txt > out.txt

You can extend the print to print your constant text at the end of the line.
Previous Topic: Urgent Help
Next Topic: Oracle 9iR2 and HP-UX 11i
Goto Forum:
  


Current Time: Mon Nov 25 21:33:39 CST 2024