Joining especific lines in "2n" lines file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining especific lines in "2n" lines file
# 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:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-12-2018 at 11: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

This User Gave Thanks to RudiC For This Post:
# 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 11:41 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 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

This User Gave Thanks to RudiC For This Post:
# 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-13-2018 at 12:03 AM..
# 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File

Hi All It's me again with another huge txt files. :confused: What I have: - I have 33 huge txt files in a folder. - I have thousands of line in this txt file which contain many the letter "x" in them. - Some of them have more than one "x" character in the line. What I want to achieve:... (8 Replies)
Discussion started by: Nexeu
8 Replies

2. Shell Programming and Scripting

Grep all lines with the string "TNS-" but skip those with "TNS-12514"

Platform: Oracle Linux 6.3 From a log file, I want to grep all lines with the pattern "TNS-" but I want to skip those with the pattern "TNS-12514" . How can I do this ? (3 Replies)
Discussion started by: John K
3 Replies

3. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

4. Shell Programming and Scripting

Filter file by length, looking only at lines that don't begin with ">"

I have a file that stores data in pairs of lines, following this format: line 1: header (preceded by ">") line 2: sequence Example.txt: >seq1 name GATTGATGTTTGAGTTTTGGTTTTT >seq2 name TTTTCTTC I want to filter out the sequences and corresponding headers for all sequences that are less... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

5. Shell Programming and Scripting

Cant get awk 1liner to remove duplicate lines from Delimited file, get "event not found" error..help

Hi, I am on a Solaris8 machine If someone can help me with adjusting this awk 1 liner (turning it into a real awkscript) to get by this "event not found error" ...or Present Perl solution code that works for Perl5.8 in the csh shell ...that would be great. ****************** ... (3 Replies)
Discussion started by: andy b
3 Replies

6. Shell Programming and Scripting

Delete till ">" is found in all lines in a file

Hi, I have a file which has lines like these : I want to trim everything from the left till ">" such that the file looks like : If you have any ideas how to do this in 1-2 commands please help. Thanks. (3 Replies)
Discussion started by: sinpeak
3 Replies

7. Shell Programming and Scripting

Cat Command on File not printing "Blank" Lines?

Hello All, I have a bash script and in it at some point I call an Expect Script that does some stuff and saves its output in a ".txt" file. Example "/path/to/my/file/Expect_Output.txt" file: notice the 2nd line is empty in the file... Data for Host-1 (192.168.1.110) Checking the... (2 Replies)
Discussion started by: mrm5102
2 Replies

8. Shell Programming and Scripting

Remove ":" and join lines in outline file

I have a vim outliner file like this: Title title 2 :Testing now :testing 2 :testing 3 title 3 :testing :ttt :ttg Is there a way to use a script or command to remove... (7 Replies)
Discussion started by: jostber
7 Replies

9. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

10. Shell Programming and Scripting

Reading lines from a file, using bash, "at" command

Hi. I have the script shown below. If I execute it form the command line it seems to work properly, but when I fun it using the unix "at" command "at -m now < ./kill-at-job.sh" It appears to hang. Below is the script, the input file, and the execution as reported in the e-mail from the "at"... (3 Replies)
Discussion started by: jbsimon000
3 Replies
Login or Register to Ask a Question