Joining lines in TXT file based on first character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining lines in TXT file based on first character
# 1  
Old 08-15-2013
Joining lines in TXT file based on first character

Hi,

I have a pipe delimeted text file where lines have been split over 2 lines and I need to join them back together. For example the file I have is similar to the following:
Code:
aaa|bbb
|ccc
ddd|eee
fff|ggg
|hhh

I ideally need to have it looking like the following
Code:
aaa|bbb|ccc
ddd|eee
fff|ggg|hhh

I just dont seem to be able to get exactly what I need!

Any help would be very much appreciated.

Cheers,

Last edited by Franklin52; 08-15-2013 at 05:01 AM.. Reason: Please use code tags
# 2  
Old 08-15-2013
try:
Code:
awk '{for(i=1; i<=NF; i++) if ($i !~ /^[|]/) {printf ($(i+1) !~ /^[|]/) ? $i "\n" : $i } else {print $i} }' RS= input

# 3  
Old 08-15-2013
Rather than write masses of shell script in a while read line loop, a pretty crummy way with vi might do the job:-
Code:
count=`grep -c "^|" $file`
i=1
( while [ $i -le $count ]
do
   echo "/^|kJ\c"
done
echo ":wq" ) | vi $file


I suspect a properly written awk or sed would be much better.




Robin

---------- Post updated at 03:51 PM ---------- Previous update was at 03:50 PM ----------

Smilie Ah, there's one on the thread already. Smilie
# 4  
Old 08-15-2013
Another awk veresion
Code:
awk '{s=(a!~/^\|/ && $0!~/^\|/)?"":"\n";printf "%s"s,$0;a=$0}' file
aaa|bbb|ccc
ddd|eee
fff|ggg|hhh

# 5  
Old 08-19-2013
A portable solution that joins any number of lines beginning with | is
Code:
sed -e ':a' -e '$b' -e 'N; s/\n|/|/; ta' -e 'P;D' file


Last edited by MadeInGermany; 08-19-2013 at 02:03 PM.. Reason: memory-efficient version
# 6  
Old 08-20-2013
Code:
awk '{printf (/^\|/)?$0:RS $0}' infile

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

2. UNIX for Dummies Questions & Answers

Select lines based on character length

Hi, I've got a file like this: 22 22:35645163:T:<CN0>:0 0 35645163 T <CN0> 22 rs140738445:20902439:TTTTTTTG:T 0 20902439 T TTTTTTTG 22 rs149602065:40537763:TTTTTTG:T 0 40537763 T TTTTTTG 22 rs71670155:50538408:TTTTTTG:T 0 50538408 T TTTTTTG... (3 Replies)
Discussion started by: zajtat
3 Replies

3. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

4. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

5. Shell Programming and Scripting

Replacing a character with a number based on lines

Hi, I am in need of help for the two things which is to be done. First, I have a file that has around four columns. The first column is filled with letter "A". There are around 400 lines in the files as shown below. A 1 5.2 3.2 A 2 0.2 4.5 A 1 2.2 2.2 A 5 2.1 ... (2 Replies)
Discussion started by: begin_shell
2 Replies

6. Shell Programming and Scripting

Joining lines in a file - help!

I'm looking for a way to join lines in a file; e.,g consider the following R|This is line 1 R|This is line 2 R|This is line 3 R|This is line 4 R|This is line 5 what i want to end up with is R|This is line 1 R|This is line 2 R|This is line 3 R|This is line 4 R|This is line 5 so... (15 Replies)
Discussion started by: Storms
15 Replies

7. Shell Programming and Scripting

bash - joining lines in a file

I’m writing a bash shell script and I want to join lines together where two variables on each line are the same ie. 12345variablestuff43212morevariablestuff 12345variablestuff43212morevariablestuff 34657variablestuff78945morevariablestuff 34657variablestuff78945morevariablestuff... (12 Replies)
Discussion started by: Cultcha
12 Replies

8. Shell Programming and Scripting

Merging lines based on occurances of a particular character in a file

Hi, Is there any way to merge two lines based on specific occurance of a character in a file. I am having a flat file which contains multiple records. Each row in the file should contain specified number of delimiter. For a particular row , if the delimiter count is not matched with... (2 Replies)
Discussion started by: mohan_tuty
2 Replies

9. Shell Programming and Scripting

Joining 2 lines in a file together

Hi guys, I've got a log file which has entries that look like this: ------------------------------------------------------------------------------- 06/08/04 07:57:57 AMQ9002: Channel program started. EXPLANATION: Channel program 'INSCCPQ1.HSMTSPQ1' started. ACTION: None. ... (3 Replies)
Discussion started by: m223464
3 Replies

10. Shell Programming and Scripting

Joining lines in log file

Hi, I need to develop a script to join multiple three lines in a log file into one line for processing with awk and grep. I looked at tr with no success. The first line contains the date time information. The second line contains the error line. The third line is a blank line. Thanks, Mike (3 Replies)
Discussion started by: bubba112557
3 Replies
Login or Register to Ask a Question

Featured Tech Videos