Concatenation of a line to previous line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenation of a line to previous line
# 1  
Old 03-02-2011
Concatenation of a line to previous line

I need to concatenate all lines of a file into 1 line.
input file containing lines like
Code:
001123456400001234563 107 001578000000000000000000000000000000000000 0000000021600 
001123456912345600003 107 001578000000000000000000000000000000000000 0000000992000

i am using command
Code:
echo `awk '{ printf ("%s",$0) }' infile`> outfile

its working fine but converting number of spaces to 1 like if file having 12 spaces its counting 1. I want record length should not altered while concatnating the lines. Kindly help.

Output file should be in one line with same number of spaces as in input file like:
Code:
001123456400001234563 107 001578000000000000000000000000000000000000 0000000021600 001123456912345600003 107 001578000000000000000000000000000000000000 0000000992000

Here also i am not able to display correct number of spaces(displaying only 1 space)


Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 03-02-2011 at 08:19 AM.. Reason: code tags
# 2  
Old 03-02-2011
Try below
Code:
paste -s inputfile

# 3  
Old 03-02-2011
This one is simple Smilie
Code:
cat infile | tr '\n' ' ' > outfile

# 4  
Old 03-02-2011
Quote:
Originally Posted by Dahu
This one is simple Smilie
Code:
cat infile | tr '\n' ' ' > outfile

The using of cat is superfluous:
Code:
tr '\n' ' ' < infile > outfile

# 5  
Old 03-02-2011
Quote:
Originally Posted by Dahu
This one is simple Smilie
Code:
cat infile | tr '\n' ' ' > outfile

Yes, this works just like my previous command...but there is a SPACE introduced btw the joined lines. Is there an option to avoid it.

example:
line 1: aaaaaaaaaa
line 2: bbbbbbbbbb
line 3: cccccccccc

output: aaaaaaaaa bbbbbbbbbb cccccccccc

Expected output: aaaaaaaaabbbbbbbbbbcccccccccc
# 6  
Old 03-02-2011
Try:
Code:
tr -d '\n' < file > newfile

or:
Code:
awk '1' ORS= file > newfile

If you want a newline at the end of the line:
Code:
awk '1;END{print "\n"}' ORS= file > newfile

# 7  
Old 03-02-2011
Code:
perl -i -pe 's/[\n\r]/ /g' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

5. Shell Programming and Scripting

Replace first field of a line with previous filed of the line

Hi Everyone, I have a file as below: IM2345638,sherfvf,usha,30 IM384940374,deiufbd,usha,30 IM323763822,cdejdkdnbds,theju,15 0,dhejdncbfd,us,20 IM398202038,dhekjdkdld,tj,30 0,foifsjd,u2,40 The output i need is as below IM2345638,sherfvf,usha,30... (4 Replies)
Discussion started by: usha rao
4 Replies

6. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

7. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

8. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

9. Shell Programming and Scripting

Append next line to previous line when one pattern not found

Hi, I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are... (4 Replies)
Discussion started by: kannansr621
4 Replies

10. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies
Login or Register to Ask a Question