The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM
Reading Multiple Variables From a Single Line in Shell Drek Shell Programming and Scripting 14 12-21-2006 11:20 AM
reading a single line rochitsharma Shell Programming and Scripting 2 02-27-2006 01:06 AM
Splitting a single line into multiple lines thanuman Shell Programming and Scripting 4 02-23-2005 04:56 AM
reading a single character in C alodha High Level Programming 3 01-28-2005 06:22 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-17-2006
braindrain braindrain is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 67
Reading multiple lines as single

Hi,
Is it possible to read every 2 lines as single record, I have a file like below,

Code:
~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

Last edited by braindrain; 10-17-2006 at 12:01 PM..
  #2 (permalink)  
Old 10-17-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
paste -s -d"\t\n" file
Above command will convert the records across two lines into singe line.
  #3 (permalink)  
Old 10-17-2006
petebear petebear is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 23
I'm sure someone will be able to get your desired output into a single field, but here it is in two:

Code:
awk 'BEGIN { RS = "/^~/" ; FS = "\n" ; OFS = "~" } {gsub(/ +/, "") ; print $1,$2 }'

Last edited by petebear; 10-17-2006 at 11:57 AM..
  #4 (permalink)  
Old 10-17-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
If you have Python:
Code:
#!/usr/bin/python
data = open("file.txt").readlines()
for i in range(len(data)):
 	try:
 		if data[i].startswith("~"):
 			data[i] = data[i].strip() #strip new lines
 			s = data[i] + "~"  + data[i+1] #concat 2 lines
 			print s.replace(" ","").strip() #replace all spaces and strip new lines
 	except: 
                        print data[i].replace(" ","") + "~"
output:
Code:
~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~
  #5 (permalink)  
Old 10-17-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
paste -d'~' - - < myFile.txt
OR
nawk -F'~' 'ORS=(FNR%2)?FS:RS' myFile.txt
  #6 (permalink)  
Old 10-17-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
paste -d'~' - - < myFile.txt
I think this will introduce an extra ~ in the output
  #7 (permalink)  
Old 10-17-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by anbu23
Code:
paste -d'~' - - < myFile.txt
I think this will introduce an extra ~ in the output
... not that I can see
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0