Moving next 2 lines contents to previous lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving next 2 lines contents to previous lines
# 1  
Old 04-25-2007
Moving next 2 lines contents to previous lines

My input file is
aaa
bbb
ccc
a1a
b1b
c1c
a2a
b2b
c2c

I want the output file to look like that:
aaa,bbb,ccc
a1a,b1b,c1c
a2a,b2b,c2c

How do I achieve this ?
# 2  
Old 04-25-2007
Code:
paste -d"," - - - file

# 3  
Old 04-25-2007
Moving next 2 lines to previous line.

Hi anbu23,

I tried ...but when I execute the paste command at shell prompt it gets stuck up for infinite time...and no outputs is created...

I am using the following :
home/myscripts > paste -d"," - - - a > b

Thanks
# 4  
Old 04-25-2007
Quote:
Originally Posted by Amruta Pitkar
Hi anbu23,

I tried ...but when I execute the paste command at shell prompt it gets stuck up for infinite time...and no outputs is created...

I am using the following :
home/myscripts > paste -d"," - - - a > b

Thanks
Try this
Code:
paste -d"," - - - <  a > b

# 5  
Old 04-25-2007
a crude awk implementation
Code:
awk ' {     
              arr[NR] = $0","
              if (NR%3==0) { 
                     sub("," , "",arr[NR])                     
                     for (i in arr) printf arr[i] 
                     print ""
                     delete arr
              }            
       }      
' "file"

# 6  
Old 04-25-2007
Moving next 2 lines to previous line.

Hello anbu23

It worked...thanks.

Amruta
# 7  
Old 04-25-2007
Quote:
Originally Posted by anbu23
Code:
paste -d"," - - - file


That should be:

Code:
paste -d"," - - - < file

Or:

Code:
printf "%s,%s,%s\n" $( cat < file )

Which, in bash and ksh, can be shortened to:

Code:
printf "%s,%s,%s\n" $( < file )


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Previous and Post lines in a pattern

Dear Community; I am posting this after looking at several solutions that were not fully relevant to the issue that I am facing. I have a large xml file, 100k+ lines which have patterns like below: <OfferDefinition Id="11"> <Type>Account</Type> ... (3 Replies)
Discussion started by: mystition
3 Replies

2. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

3. Shell Programming and Scripting

Append next line to previous lines when NF is less than 0

Hi All, This is very urgent, I've a data file with 1.7 millions rows in the file and the delimiter is cedilla and I need to format the data in such a way that if the NF in the next row is less than 1, it will append that value to previous line. Any help will be appricated. Thanks,... (17 Replies)
Discussion started by: cumeh1624
17 Replies

4. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

5. Shell Programming and Scripting

Help Moving lines

Hi, I tried going through the existing threads, but couldn't find a solution to my problem. My input file is: 199.15.2.7|gxi.wxxx.com/|(Mozilla/4.0)| ASCII text|950604ad05 70.38.9.7|mmg10.com/2011/1.gif|(Mozilla/4.0)| application/x-ms-dos-executable|a2f332Y02d0... (5 Replies)
Discussion started by: r4v3n
5 Replies

6. Shell Programming and Scripting

grep lines from the previous day's file

I have files called printfile.log.1121, printfile.log.1122, etc where 1121 and 1122 are the date of the file creation. The file contents are like: ================================================================================ User = d_prod Env = d_prod Dir =... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

7. Shell Programming and Scripting

ksh comparing current and previous lines

Hi, I am currently trying to work out how to compare one line with the last line I have read in via ksh. I have a file which has sorted output from a previous sort command so all the lines are in order already and the file would look something like show below. Each line has a name and a time... (5 Replies)
Discussion started by: paulie
5 Replies

8. Shell Programming and Scripting

Find pattern a delete previous 5 lines

Hi guys, i have the follow problem i need to delete 10 row before the pattern and 1 after and the pattern row itself. file looks like: frect 9.8438 25.8681 10.625 25 . dynprop \ (# \ (call fox_execute(__self))) \ (FOX_VAR_29 \ ... (4 Replies)
Discussion started by: EjjE
4 Replies

9. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

10. Shell Programming and Scripting

Need to delete previous lines

Need to delete the line which is directly above any line which has 3 fields in it. one two three one two three four five six four five six seven eight nine seven eight nine one two three should output: one two three (7 Replies)
Discussion started by: linuxkid
7 Replies
Login or Register to Ask a Question