Hi,
Is it possible to read every 2 lines as single record, I have a file like below,
~CZK ~TSCHECHISCHE KRONE ~TSCHECH. REPUBLIK
Dec 1 2005 12:00AM~ 10.840000~
~DKK ~DAENISCHE KRONE ~DAENEMARK
Dec 2 2005 12:00AM~ 17.451000~
~DZD ~ALGERISCHER DINAR ~ALGERIEN
Dec 3 2005 12:00AM~ 16.100000~
~EEK ~ESTNISCHE KRONE ~ESTLAND
and I want to read them as single record separated by Tilda's.
~CZ~TSCHECHISCHE KRONE~TSCHECH. REPUBLIK~Dec 1 200512:00AM ~10.840000~
i.e something the does the opposite of fold.
Is this possible to do in awk or ????
Thanks a lot
anbu23
October 17, 2006, 11:44am
2
paste -s -d"\t\n" file
Above command will convert the records across two lines into singe line.
I'm sure someone will be able to get your desired output into a single field, but here it is in two:
awk 'BEGIN { RS = "/^~/" ; FS = "\n" ; OFS = "~" } {gsub(/ +/, "") ; print $1,$2 }'
If you have Python:
#!/usr/bin/python
data = open("file.txt").readlines()
for i in range(len(data)):
try:
if data.startswith("~"):
data = data.strip() #strip new lines
s = data + "~" + data[i+1] #concat 2 lines
print s.replace(" ","").strip() #replace all spaces and strip new lines
except:
print data.replace(" ","") + "~"
output:
~CZK~TSCHECHISCHEKRONE~TSCHECH.REPUBLIK~Dec1200512:00AM~10.840000~
~DKK~DAENISCHEKRONE~DAENEMARK~Dec2200512:00AM~17.451000~
~DZD~ALGERISCHERDINAR~ALGERIEN~Dec3200512:00AM~16.100000~
~EEK~ESTNISCHEKRONE~ESTLAND~
paste -d'~' - - < myFile.txt
OR
nawk -F'~' 'ORS=(FNR%2)?FS:RS' myFile.txt
anbu23
October 17, 2006, 1:20pm
6
paste -d'~' - - < myFile.txt
I think this will introduce an extra ~ in the output
anbu23
October 17, 2006, 2:59pm
8
vgersh99:
... not that I can see
~CZK ~TSCHECHISCHE KRONE ~TSCHECH. REPUBLIK~ Dec 1 2005 12:00AM~ 10.840000~
~DKK ~DAENISCHE KRONE ~DAENEMARK~ Dec 2 2005 12:00AM~ 17.451000~
~DZD ~ALGERISCHER DINAR ~ALGERIEN~ Dec 3 2005 12:00AM~ 16.100000~
~EEK ~ESTNISCHE KRONE ~ESTLAND~
isn't it what the OP wanted?
stobo
October 18, 2006, 10:16am
10
paste -d'~' - - <myfile | tr -s ' '| sed -e s/\ \~/\~/g -e s/\~\ /\~/g