Home Man
Search
Today's Posts
Register

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

Tags
awk, sed, shell scripts, shell while read loop, solved

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 04-12-2018
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.
# 2  
Old 04-12-2018
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)
# 3  
Old 04-12-2018
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 04-12-2018
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)
# 5  
Old 04-12-2018
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..
# 6  
Old 04-12-2018
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)
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Grep all lines with the string "TNS-" but skip those with "TNS-12514" John K Shell Programming and Scripting 3 12-09-2013 12:11 PM
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
Cant get awk 1liner to remove duplicate lines from Delimited file, get "event not found" error..help andy b Shell Programming and Scripting 3 04-09-2013 04:07 AM
Delete till ">" is found in all lines in a file sinpeak Shell Programming and Scripting 3 09-13-2012 11:05 AM
Cat Command on File not printing "Blank" Lines? mrm5102 Shell Programming and Scripting 2 07-26-2012 04:02 PM
Remove ":" and join lines in outline file jostber Shell Programming and Scripting 7 11-20-2010 05:22 PM
Need to parse file "x" lines at a time ... awk array? STN Shell Programming and Scripting 7 01-27-2010 03:51 PM
Reading lines from a file, using bash, "at" command jbsimon000 Shell Programming and Scripting 3 03-17-2009 03:53 PM


All times are GMT -4. The time now is 02:00 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password