Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Joining especific lines in "2n" lines file

Shell Programming and Scripting


Tags
awk, sed, shell while read loop, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-12-2018   -   Original Discussion by felino
felino's Unix or Linux Image
felino felino is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 12 April 2018, 4:47 PM EDT
Location: Santiago de Cuba
Posts: 2
Thanks: 3
Thanked 0 Times in 0 Posts
Joining especific lines in "2n" lines file

Hi to everybody.

I have a "2n" lines file. I would like to create a
new file with only "n" lines, each line in the new
file formed by the proper odd line of the old file
joined with the following even line (separated by
a space) of the old file. I'd prefer using sed or
bash.

-example-

Old file:



Code:
line-01
line-02
line-03
line-04

...

New file:



Code:
line01 line02
line03 line04

...

Thank you in advance.

felino
(Cuban, Lousy Internet, New to this site)


Moderator's Comments:
Joining especific lines in "2n" lines file Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-12-2018 at 10:33 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 04-12-2018   -   Original Discussion by felino
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 May 2018, 5:24 PM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,922 Times in 3,606 Posts
Welcome to the forum.

Does it HAVE to be sed or bash?


Code:
awk 'ORS=NR%2?"\t":"\n"' file
line-01    line-02
line-03    line-04


OK, sed is possible, and simple, too:


Code:
sed 'N; s/\n/\t/' file
line-01    line-02
line-03    line-04

The Following User Says Thank You to RudiC For This Useful Post:
felino (04-12-2018)
Sponsored Links
    #3  
Old Unix and Linux 04-12-2018   -   Original Discussion by felino
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 26 May 2018, 6:35 PM EDT
Location: Amsterdam
Posts: 11,810
Thanks: 543
Thanked 3,441 Times in 3,034 Posts
Or can it be paste?


Code:
paste - - < file



--
Note: \t in the replacement part of the "s" command is GNU sed only. For regular sed use actual TAB characters (CTRL-V TAB).

Last edited by Scrutinizer; 04-12-2018 at 10:41 AM..
The Following 2 Users Say Thank You to Scrutinizer For This Useful Post:
felino (04-12-2018), RudiC (04-12-2018)
    #4  
Old Unix and Linux 04-12-2018   -   Original Discussion by felino
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 May 2018, 5:24 PM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,922 Times in 3,606 Posts
Or bash:


Code:
while read L1; do read L2; echo $L1 $L2; done < file
line-01 line-02
line-03 line-04

The Following User Says Thank You to RudiC For This Useful Post:
felino (04-12-2018)
Sponsored Links
    #5  
Old Unix and Linux 04-12-2018   -   Original Discussion by felino
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 26 May 2018, 6:35 PM EDT
Location: Amsterdam
Posts: 11,810
Thanks: 543
Thanked 3,441 Times in 3,034 Posts
To do it in shell it is best to turn off field splitting (that also removes leading and trailing whitespace) and other interpretations by the shell:
  • switch off splitting IFS (IFS=)
  • ignore \ characters in the input (-r option)
  • protect variables against field splitting when printing (use double quotes)
  • protect against special characters when printing (use printf instead of echo)


Code:
while IFS= read -r L1 
do
  IFS= read -r L2
  printf "%s\t%s\n" "$L1" "$L2"
done < file


Last edited by Scrutinizer; 04-12-2018 at 11:03 PM..
Sponsored Links
    #6  
Old Unix and Linux 04-12-2018   -   Original Discussion by felino
felino's Unix or Linux Image
felino felino is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 12 April 2018, 4:47 PM EDT
Location: Santiago de Cuba
Posts: 2
Thanks: 3
Thanked 0 Times in 0 Posts
I want to thank to Mr RudiC
and Mr. Scrutinizer for their
precise and accurate answers
to my problem.

And to Mr. RudiC for correcting
my improper post.

Thank you again.

Felino
(Cuban, Lousy Internet, New to this site)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File Nexeu UNIX for Dummies Questions & Answers 8 02-10-2015 03:47 AM
Find lines with "A" then change "E" to "X" same line nightwatchrenba Shell Programming and Scripting 10 08-28-2013 05:00 AM
Filter file by length, looking only at lines that don't begin with ">" pathunkathunk Shell Programming and Scripting 2 07-23-2013 12:23 AM
Cat Command on File not printing "Blank" Lines? mrm5102 Shell Programming and Scripting 2 07-26-2012 04:02 PM
Need to parse file "x" lines at a time ... awk array? STN Shell Programming and Scripting 7 01-27-2010 03:51 PM



All times are GMT -4. The time now is 09:05 PM.