Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Using Sed/Awk for File Processing

Status
Not open for further replies.

cdwells

Technical User
Sep 3, 2003
5
US
I have a data file such containing text such as below:

FIELD1~FIELD2~"FIELD3"~FIELD4-"$$$$"
FIELD1~FIELD2~"FIELD3"~FIELD4-"$$$$"
FIELD1~FIELD2~"FIELD3"~FIELD4-"$$$$"
FI
ELD1~FIELD2~"FIELD3"~FIELD4~"$$$$"

Some of the records are broken so I need to first remove all newline characters. Once this has been fixed, the characters ~"$$$$" will denote when my record line (will end) so I will need to replace all occurrences of ~"$$$$" with a newline character. My only problem is that I am not familiar with handling the newline character \n in sed or awk. Any suggestions?
 
Try something like this:
Code:
awk -F'~' '{buf=buf $0}
$NF=="\"$$$$\""{print buf;buf=""}
' /path/to/datafile


Hope This Help
PH.
 
Thanks for the advice, however doesn't seem to be working. If anyone could give me advice as to stripping newline characters from a file using sed, that may steer me in the right direction (ie:

this
is
a
test
file

could become "this is a test file"

how would I go about accomplishing that (removing the newlines) using sed?
 
Tested with your 1st data example:
Code:
awk -F'~' '{buf=buf $0}
$NF~/"\$\$\$\$"$/{print buf;buf=""}
' /path/to/datafile
The problem was that some records have - instead of ~
Anyway, to strip ALL new-lines:
Code:
tr -d '\012' <inputfile

Hope This Help
PH.
 
Awk and sed possible solutions :

awk -F'~' '
buf=buf $0}
$NF~/-&quot;\$\$\$\$&quot;$/{ sub(/-&quot;\$\$\$\$&quot;$/,&quot;&quot;,buf);print buf;buf=&quot;&quot;}
' /path/to/datafile


sed -e '
:join
/-&quot;\$\$\$\$&quot;$/!{N
s/\\\n//
b join
}
s/-&quot;\$\$\$\$&quot;$//
' /path/to/datafile


Jean Pierre.
 
Forgot to get rid of trailing $:
Code:
awk -F'~' '{buf=buf $0}
$NF~/&quot;\$\$\$\$&quot;$/{
 print substr(buf,1,length(buf)-7);buf=&quot;&quot;
}' /path/to/datafile


Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top