Connecting Tech Pros Worldwide Forums | Help | Site Map

marge line

Newbie
 
Join Date: Feb 2008
Posts: 27
#1: Feb 28 '08
test.text:-
--------
601215856422|COM|0|0|CD|1|3.99
601215868029|COM|0|0|CD|1|3.99
801472092322|COM|0|0|CD|2|10.98
802097013822|COM|0|0|CD|2|10.98
601215856422|MOM|0|0|CD|1|3.99
601215856422|RNO|0|0|CD|1|3.99
601215856422|SOM|0|0|CD|1|3.99

..........

I have to do
601215856422|COM|0|CD|1|3.99|MOM|0|CD|1|3.99|RNO|0 |CD|1|3.99
....

using shell script

pls help me.

thanks!
vaskar

kaarthikeyapreyan's Avatar
Member
 
Join Date: Apr 2007
Location: India
Posts: 101
#2: Apr 2 '08

re: marge line


Quote:

Originally Posted by vaskarbasak

test.text:-
--------
601215856422|COM|0|0|CD|1|3.99
601215868029|COM|0|0|CD|1|3.99
801472092322|COM|0|0|CD|2|10.98
802097013822|COM|0|0|CD|2|10.98
601215856422|MOM|0|0|CD|1|3.99
601215856422|RNO|0|0|CD|1|3.99
601215856422|SOM|0|0|CD|1|3.99

..........

I have to do
601215856422|COM|0|CD|1|3.99|MOM|0|CD|1|3.99|RNO|0 |CD|1|3.99
....

The code is bit complicating but i have provided you flexibility to do a lot of things,I also believe there are a lot more simpler ways to do it

Expand|Select|Wrap|Line Numbers
  1. a=601215856422
  2. filelengthmain=`wc -l input.txt |cut -d ' ' -f1`
  3. val1=1
  4. val1=`expr $val1 + 0`
  5. for ((j=1; j<=$filelengthmain; j++))
  6. do
  7. ln=`head -$val1 input.txt|tail -1`
  8. val=`echo $ln | cut -d '|' -f 1`
  9. if test $a = $val
  10. then
  11. fvals[$val]=`echo $ln | grep -e $a |grep -v SOM | cut -d '|' -f 2-3,5-7`
  12. b=`echo $b"|"${fvals[$val]}`
  13. fi
  14. val1=`expr $val1 + 1`
  15. done
  16. echo $a$b
  17.  
Output :

601215856422|COM|0|CD|1|3.99|MOM|0|CD|1|3.99|RNO|0 |CD|1|3.99|
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#3: Apr 2 '08

re: marge line


You can use this script

Expand|Select|Wrap|Line Numbers
  1. FULL_LINE=""
  2.  
  3. while read LINE
  4. do
  5.   if [ "$FULL_LINE" == "" ]
  6.   then
  7.       FULL_LINE="$LINE"
  8.   else
  9.     FULL_LINE=$FULL_LINE"|"$LINE
  10.   fi
  11. done < "ip.txt" 
  12.  
  13. echo "FULL_LINE is : ${FULL_LINE}"
  14.  
  15.  
Raghuram
radoulov's Avatar
Member
 
Join Date: Jun 2007
Posts: 34
#4: Apr 2 '08

re: marge line


If I understand correctly:

Expand|Select|Wrap|Line Numbers
  1. awk '{ 
  2. for (f=2; f<=NF; f++)
  3.   if (f != 3)
  4.     x[$1] = x[$1] FS $f
  5. } END {
  6. for (k in x)
  7.   print k x[k]
  8. }' OFS='|' FS='|' file
Newbie
 
Join Date: Mar 2008
Posts: 14
#5: Apr 2 '08

re: marge line


Here is a one-liner for the same job:
Expand|Select|Wrap|Line Numbers
  1. cat test.text | echo -n $(awk {'print $0 "|"'}) | sed -e 's/\ *//g'
The -n is to remove newline, and the last sed is to remove a whitespace that would otherwise sneak in.
Newbie
 
Join Date: Feb 2008
Posts: 27
#6: Apr 29 '08

re: marge line


Thanks all!

601215856422|COM|0|0|CD|1|3.99
601215868029|COM|0|0|CD|1|3.99
801472092322|COM|0|0|CD|2|10.98
802097013822|COM|0|0|CD|2|10.98
601215856422|MOM|0|0|CD|1|3.99
601215856422|RNO|0|0|CD|1|3.99
601215856422|SOM|0|0|CD|1|3.99

but i have to do

601215856422|MOM|1|3.99|RNO|1|3.99|SOM|1|3.99|COM| 1|3.99
801472092322|COM|0|0|CD|2|10.98
601215868029|COM|0|0|CD|1|3.99
.....

the file size is very large nearly 91 MB.So it takes lot of time.But i have to do it with in 5 min.

Still i am trying to solve the problem.

please help me...
Newbie
 
Join Date: Mar 2008
Posts: 14
#7: Apr 29 '08

re: marge line


Quote:

Originally Posted by vaskarbasak

601215856422|MOM|1|3.99|RNO|1|3.99|SOM|1|3.99|COM| 1|3.99
801472092322|COM|0|0|CD|2|10.98
601215868029|COM|0|0|CD|1|3.99

Your last post didn't really clearify what you are looking for. If you could please add a few lines of explanation, then you would have a greater chance of getting the right answer.
Newbie
 
Join Date: Feb 2008
Posts: 27
#8: Apr 30 '08

re: marge line


i need concate all duplicate lines like

794051400123|COM|24|MOM|62|SOM|25|RNO|73
794051413727|COM|11||MOM|4|RNO|8
............

and save it another file..
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#9: May 2 '08

re: marge line


Quote:

Originally Posted by vaskarbasak

i need concate all duplicate lines like

794051400123|COM|24|MOM|62|SOM|25|RNO|73
794051413727|COM|11||MOM|4|RNO|8
............

and save it another file..


The first part of the string that is the 794051400123 is the token with which you find whether it is a duplicate token?

Raghuram
Reply


Similar Unix / Linux / BSD bytes