Combining two lines into one, UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining two lines into one, UNIX
# 1  
Old 10-21-2015
Combining two lines into one, UNIX

Hi All,

I have a file which has the following sample lines

Code:
--
<Member 	name="Canada"
Currency="CAD"
--
<Member 	name="UK"
Currency="GBP"
--
<Member 	name="Switzerland"
Currency="CHF"
--
<Member 	name="Germany"
Currency="EUR"
--

My requirement is to have a comma separated file, in the following format

Code:
Member name,Canada,Currency,CAD
Member name,UK,Currency,GBP ...

Thanks,
Dev
# 2  
Old 10-21-2015
Hello Dev,

Could you please try following and let me know if this helps you.
Code:
awk '/<Member/{sub(/</,X,$0);sub(/[[:space:]]+/," ",$0);sub(/=\"/,OFS,$0);sub(/\"/,X,$0);A=$0} /Currency/{sub(/\=\"/,OFS,$0);sub(/\"/,X,$0);print A OFS $0}' OFS=,  Input_file

Output will be as follows.
Code:
Member name,Canada,Currency,CAD
Member name,UK,Currency,GBP
Member name,Switzerland,Currency,CHF
Member name,Germany,Currency,EUR

EDIT: Adding a non one-liner form of solution for same.
Code:
awk '/<Member/{
                sub(/</,X,$0);
                sub(/[[:space:]]+/," ",$0);
                sub(/=\"/,OFS,$0);
                sub(/\"/,X,$0);
                A=$0
              }
     /Currency/{
                sub(/\=\"/,OFS,$0);
                sub(/\"/,X,$0);
                print A OFS $0
               }
    ' OFS=,  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-21-2015 at 07:16 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-21-2015
Hi.

With fgrep, paste, sed:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate combining and transforming data, sed.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C fgrep paste sed

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
fgrep -v -e '--' $FILE |
tee f1 |
paste -d"=" - - |
tee f2 |
# sed 's/^.// ; s/[ 	][ 	]*/ /g ; s/=/,/g ; s/"//g'
sed 's/^.// ; s/[[:space:]][[:space:]]*/ /g ; s/=/,/g ; s/"//g'

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
fgrep GNU grep 2.5.3
paste (GNU coreutils) 6.10
sed GNU sed version 4.1.5

-----
 Input data file data1:
--
<Member 	name="Canada"
Currency="CAD"
--
<Member 	name="UK"
Currency="GBP"
--
<Member 	name="Switzerland"
Currency="CHF"
--
<Member 	name="Germany"
Currency="EUR"
--

-----
 Results:
Member name,Canada,Currency,CAD
Member name,UK,Currency,GBP
Member name,Switzerland,Currency,CHF
Member name,Germany,Currency,EUR

This User Gave Thanks to drl For This Post:
# 4  
Old 10-21-2015
Code:
sed -n -r '/<Member/{N; s/<(Member).*(name)="(.*)"\n(.*)="(.*)".*$/\1 \2,\3,\4,\5 /p}'

This works fine!

For some reason, I was not able to do it using the
Code:
[:alpha:]

or
Code:
[[:alpha:]]

It seemed to be confuse with
Code:
="

This User Gave Thanks to Klasform For This Post:
# 5  
Old 10-21-2015
What operating system are you using?

If you're using a Solaris/SunOS system try changing sed to /usr/xpg4/bin/sed to get a version that recognizes character classes (e.g., [[:alpha:]]).
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 10-21-2015
Using sed (GNU sed) 4.2.2
This User Gave Thanks to Klasform For This Post:
# 7  
Old 10-22-2015
Another approach:
Code:
awk '$1~/^Member/{gsub(/\n/,x); print $1,$2,$3,$4}' FS='[=]*"' OFS=, RS=\<  file

Code:
Member 	name,Canada,Currency,CAD
Member 	name,UK,Currency,GBP
Member 	name,Switzerland,Currency,CHF
Member 	name,Germany,Currency,EUR

---
Or if you are OK with leaving redundant information out of the comma delimited file, try:
Code:
awk '$1~/^Member/{print $2,$4}' FS=\" OFS=, RS=\< file

Code:
Canada,CAD
UK,GBP
Switzerland,CHF
Germany,EUR

This User Gave Thanks to Scrutinizer 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

Combining lines in to one line

Hi Friends, I have a file1.txt 1001 jkilo yrhfm 200056 jhdf rjhwjkrh 3+u8jk5h3 uru ehjk 1002 jkfhk hfjkd 2748395 fdjksfh hefjkh 3hdfk ejkh kjhjke In the above if you see the firt charcter of each line mentioned in red has a pattern . I need to create another file where , the... (6 Replies)
Discussion started by: i150371485
6 Replies

2. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

3. Shell Programming and Scripting

Combining lines between two specific lines

Hi, I have a requirement like following: I have input file like: Question: 1 ----Multiple choice--- What is your favourite colour? Options: a) red b) blue c) none of these Question: 2 ---Multiple choice----- In which month did you join your first job? Options: a) Jan b) Feb c)... (11 Replies)
Discussion started by: ppatra
11 Replies

4. Shell Programming and Scripting

Combining many lines to one using awk or any unix cmd

Combining many lines to one using awk or any unix cmd Inputfile: Output : Appreciate help on this. (14 Replies)
Discussion started by: pinnacle
14 Replies

5. Shell Programming and Scripting

Combining two text lines into one

Here is the sample file: 646 STARTED Tue Dec 30 06:38:53 2008 Job DdCustXAddr_brad has been reset. 647 STARTED Tue Dec 30 06:38:54 2008 Starting Job DdCustXAddr_brad. (...) 704 STARTED Tue Dec 30 06:49:02 2008 Job DdCustXAddr_brad aborted. I want to combine every non-numbered line... (4 Replies)
Discussion started by: mfavero
4 Replies

6. Shell Programming and Scripting

searching thru or combining multiple lines in a unix file

This is the problem actually: This regex: egrep "low debug.*\".*\"" $dbDir/alarmNotification.log is looking for data between the two quotation marks: ".*\" When I hate data like this: low debug 2009/3/9 8:30:20.47 ICSNotificationAlarm Prodics01ics0003 IC... (0 Replies)
Discussion started by: ndedhia1
0 Replies

7. Shell Programming and Scripting

combining lines in files

hey, I want to combine every three lines in a file onto one i.e old file: 82 67 32 62 58 39 29 47 58 27 34 50 27 35 69 38 58 70 new file: 82 67 32 62 58 39 29 47 58 27 34 50 27 25 69 38 58 70 At the moment I am using the following code: gawk 'BEGIN {x=0} { if(x<3)... (14 Replies)
Discussion started by: sme
14 Replies

8. Shell Programming and Scripting

help combining lines in awk

I seem to have gotten myself in over my head on this one. I need help combining lines together. I have a text file containing 24,000 lines (exactly why I need awk) due to bad formatting it has separated the lines (ideally it should be 12,000 lines total). Example of file: ... (2 Replies)
Discussion started by: blueheed
2 Replies

9. Shell Programming and Scripting

need help appending lines/combining lines within a file...

Is there a way to combine two lines onto a single line...append the following line onto the previous line? I have the following file that contains some blank lines and some lines I would like to append to the previous line... current file: checking dsk c19t2d6 checking dsk c19t2d7 ... (2 Replies)
Discussion started by: mr_manny
2 Replies

10. Shell Programming and Scripting

Combining multiple lines

I am fairly new to scripting. But I have been able to extract and format all of my information required into one file. My issue is that one character is on a separate line. I need to be able to add the character to the previous line. ex. abcdefghi 1 bcdefghij 3 cdefghijk 4 need to... (4 Replies)
Discussion started by: DUST
4 Replies
Login or Register to Ask a Question