Join the line on delimiter using sed/awk in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join the line on delimiter using sed/awk in UNIX
# 1  
Old 03-04-2016
Join the line on delimiter using sed/awk in UNIX

I've input as ,
Code:
abcd|  
ef  
123456|   
78|  
90

Desired output as,
Code:
abcdef  
1234567890

Anyone please give the solution.

Last edited by Don Cragun; 03-10-2016 at 03:37 AM.. Reason: Add CODE tags.
# 2  
Old 03-04-2016
Hello jinixvimal,

Welcome to forums, please use code tags for commands/inputs/codes which you are using in your posts as per forum rules. Could you please let me know if this helps you.
Code:
awk '{ORS=$0 ~ /\|$/?ORS="":"\n";sub(/\|/,X,$0);print}'  Input_file

Output will be as follows.
Code:
abcdef
1234567890

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-04-2016
Great Thank you R.Sing.. It works...
# 4  
Old 03-04-2016
try also:
Code:
awk '{printf (sub(/\| *$/, "")) ? $0 : $0 "\n";}' infile


Last edited by rdrtx1; 03-04-2016 at 12:18 PM..
# 5  
Old 03-04-2016
bash
Code:
while read -r line
do
if [[ "$line" =~ "|" ]]
then
x=`echo $line | sed s/\|//`
printf "$x"
else
printf "$line\n"
fi
done < file

# 6  
Old 03-09-2016
If printing an unknown string with printf, then always with %s!
Otherwise, it will fail if the string contains a %
Code:
awk '{printf "%s", sub(/\|$/, "") ? $0 : $0 "\n";}' inputfile

Code:
awk 'sub(/\|$/, "") {printf "%s",$0; next} 1' inputfile

Code:
while IFS= read -r line; do if [[ "$line" == *"|" ]]; then printf "%s" "${line%%|}"; else printf "%s\n" "$line"; fi; done < inputfile

A sed solution:
Code:
sed '
:L
/|$/{
$!N; s/|\n//; tL
}
' inputfile


Last edited by MadeInGermany; 03-10-2016 at 10:59 AM.. Reason: Make the sed code print an incomplete last line
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

2. Shell Programming and Scripting

Join lines using sed or awk

Hi, I have text file that looks like this: blabla bla PATTERN LINE1 LINE2 bla bla bla PATTERN LINE1 LINE2 bla PATTERN LINE1 LINE2 bla (9 Replies)
Discussion started by: hench
9 Replies

3. Shell Programming and Scripting

Need next line as a space delimiter in awk

Hi,Below is the output for p3fi_dev services 1/app/oracle> . ./oraprofile_p3fi_dev p3fi_dev_01 (P):/devoragridcn_01/app/oracle> srvctl config service -d p3fi_dev p3fi_p3fi_dev.world PREF: p3fi_dev_01 AVAIL: p3fi_dev_02 pplnet_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02 nexus_p3fidev PREF:... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

4. UNIX for Dummies Questions & Answers

UNIX one line cmd join 2 sets of data from 2 files

Hi all, This is my first and undoubtedly many posts to come. I'm new to using unix and would like a hand with this problem I have. What i'm trying to do is match 2 sets of data from 2 files and put result into file 3. Sounds simply but there is a catch, the match is a "partial field" match, if... (2 Replies)
Discussion started by: tugar
2 Replies

5. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

6. Shell Programming and Scripting

Implement in one line sed or awk having no delimiter and file size is huge

I have file which contains around 5000 lines. The lines are fixed legth but having no delimiter.Each line line contains nearly 3000 characters. I want to delete the lines a> if it starts with 1 and if 576th postion is a digit i,e 0-9 or b> if it starts with 0 or 9(i,e header and footer) ... (4 Replies)
Discussion started by: millan
4 Replies

7. Shell Programming and Scripting

how to implement in one-line awk in a fixed file having no delimiter

Hi, I have a file a.txt having no delimiter. I want to exclude the line which contains 435th character as 1 or 2 and redirect the rest of the lines to another file b. Can you pls suggest how to do this in one liner awk. Following is just one line of the input file a:- 120110116 ... (10 Replies)
Discussion started by: millan
10 Replies

8. Shell Programming and Scripting

Fixed Width Join & Pad Sed/Awk Help

I was wondering someone might be able to push me in the right direction, I am writing a script to modify fixed-width spool files, As you can see below the original spool file broke a single line into two for printability sake. I have had been able do the joins using sed, the thing I am... (10 Replies)
Discussion started by: Cho Nagurai
10 Replies

9. Shell Programming and Scripting

awk, join or sed

$ cat file1 a:23:43 A B C a:24:21 a:23:44 S D A F a:24:44 a:23:45 S D E a:24:45 $ cat file2 a:23:53 (1 Reply)
Discussion started by: jkl_jkl
1 Replies

10. Shell Programming and Scripting

awk,nawk,sed, delimiter |~|

RECORD=NEW|~|VENDORN=LUCENT|~|VENDORM=CBX500_REAR|~|NETWORK=ATM|~|SUBNETWORK=N/A|~|SITE=CIL|~|REGION=KN|~|COUNTRY=PS|~|SWITCH=SWITCH1|~|E THERNET=N/A|~|LOOPBACK=N/A|~|SHELF=N/A|~|SLOT=14|~|SUBSLOT=N/A|~|STSCHAN=N/A|~|PORT=S14|~|DS1SLOT=N/A|~|LINE=N/A|~|LPORTID=N/A|~|CARDDESC=N/A|~|CARDTYPE=BAC2RT0... (7 Replies)
Discussion started by: knijjar
7 Replies
Login or Register to Ask a Question