LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2012, 02:18 PM   #1
meetmefar
LQ Newbie
 
Registered: Aug 2005
Distribution: Linspire
Posts: 22

Rep: Reputation: 0
Concatenate the first 2 lines of a file


Hi Gurus,
I have a text file with many lines in it. I would like just to concatenate the first 2 lines and the remaining lines as it is into a new file.

Input file:

abcdef
stuvw
pqrst
xyz

output:

abcdefstuvw
pqrst
xyz

I am able to do it with looping throigh the lines and printing on to the new file. Is it possible to get a sed or perl command to do it?

Thanks,
meetmefar
 
Old 08-01-2012, 02:43 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
I don't have a linux machine with me to experiment on (holidays, y'see ) but you could do it with awk; something like (absolutely untested)

Code:
awk 'BEGIN={acc = "";} { if(NR == 1){ acc = $0 } else if (NR == 2){ print acc, $0 } else { print $0 } }' infile
 
Old 08-01-2012, 02:52 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

With sed:
Code:
$ cat infile
abcdef
stuvw
pqrst
xyz
$ sed '1{N;s/\n//}' infile
abcdefstuvw
pqrst
xyz
If we are at 1st line, then append next line (N) to pattern space (now pattern space looks like "line1\nline2") and remove newline (\n) character.

Last edited by firstfire; 08-01-2012 at 02:56 PM.
 
2 members found this post helpful.
Old 08-02-2012, 09:07 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I recommend using ed for this instead.

Code:
printf '%s\n' '1,2j' 'w newfile.txt' | ed -s infile.txt
The 'j' command joins lines together. The 'w' command writes the changes to a file. Used alone it writes back to the original, or with an argument you can create a new file.

Use '%p' to print the modifications to stdout.

Note also that if you need a space or something between the merged lines you'll need to add another command to insert it first.

Code:
printf '%s\n' '1s/$/ /' '1,2j' 'w newfile.txt' | ed -s infile.txt

How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
 
Old 08-02-2012, 09:22 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by David the H. View Post
I recommend using ed for this instead. ...
It is helpful for newbies (such as me) to know why you recommend something. Faster execution? More readable code? Something else?

Daniel B. Martin
 
2 members found this post helpful.
Old 08-02-2012, 09:50 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Maybe I shouldn't have used the word "instead". Either way will work just fine.

I like ed because it's light, the expressions needed in this case are clearer and more intuitive, and you can write out directly to a file, even back to the original. Of course you do need to use a pipe or similar to feed the commands to ed through stdin, so perhaps it's a wash.
 
1 members found this post helpful.
Old 08-03-2012, 02:04 PM   #7
meetmefar
LQ Newbie
 
Registered: Aug 2005
Distribution: Linspire
Posts: 22

Original Poster
Rep: Reputation: 0
Thank you everyone who contributed. I used the sed command, may be because I am used to it.

meetmefar
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate multiple patches into one file bryanvick Linux - Software 1 11-07-2011 12:36 PM
concatenate data from different lines kiloman Linux - General 1 03-01-2011 07:15 AM
How to concatenate output of two commands to a file starmonche Linux - General 3 05-01-2009 11:33 AM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 10:43 AM
Concatenate binary file iftiuk Linux - General 6 05-16-2005 08:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration