sed: How to modify files in a complex way


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: How to modify files in a complex way
# 15  
Old 12-08-2009
Hello Ahmad,

That is the point, I need more of a generic code, cause the rows gonna be flexible and so as the field name like |head.queue|... , they are not fixed, it can be anything written :-(
# 16  
Old 12-08-2009
The below code is for general cases (full option) Smilie :-

Code:
nawk -F"\=" '
/^[^ *$]/{b=b"|"$1 ; s=s"|"$2}                        
/^[ ]*$/{print b,"\n",s ; b="" ;s=""}
' file.txt

SmilieSmilieSmilieSmilie
# 17  
Old 12-08-2009
Hello Ahmad,

Thank you so much for the help.

Can you help me out with this, now I got the result as:

|head.queue|head.source|head.definition|rtf.edit|rtf.task|rft.cut|rft.abc|htc.lom
|abc|source|divine|line4|5|true||
|head.queue|head.source|head.definition|rtf.edit|rtf.task|rft.cut|rft.abc|htc.lom
|def|source|divine|line3||true||

Can I have something like :
|head.queue|head.source|head.definition|rtf.edit|rtf.task|rft.cut|rft.abc|htc.lom
abc|source|divine|line4|5|true||

Thank you very much
# 18  
Old 12-08-2009
Quote:
Originally Posted by pinkypunky
Hello Ahmad,

That is the point, I need more of a generic code, cause the rows gonna be flexible and so as the field name like |head.queue|... , they are not fixed, it can be anything written :-(
you just say that the filed queue will be different as above why you want to eliminate them?
and also you said that the paragraphs is dynamic !!



if you can assure that any 2 raw variable (head.queue) are the same in each paragraph you can get your desired o/p by adding the below to the code provided to you.

Code:
nawk -F"\=" '
/^[^ *$]/{b=b"|"$1 ; s=s"|"$2}
/^[ ]*$/{print b,"\n",s ; b="" ;s=""}
' file.txt | nawk -F"|" '!a[$1$2]++'

Code:
!a[$1$2]++ is just for example you can put the raw numbers that exist in each paragraph.

SmilieSmilieSmilieSmilie
# 19  
Old 12-08-2009
opps... so sorry

What I mean is, instead of this:

|head.queue|head.source|head.definition|rtf.edit|rtf.task|rft.cut|rft.abc|htc.lom
|abc|source|divine|line4|5|true||
|head.queue|head.source|head.definition|rtf.edit|rtf.task|rft.cut|rft.abc|htc.lom
|def|source|divine|line3||true||

Can I have something like :
|head.queue|head.source|head.definition|rtf.edit|rtf.task|rft.cut|rft.abc|htc.lom
abc|source|divine|line4|5|true||
def|source|divine|line3||true||

Without that the |head.queue|... line will be repeated for each of the data value

Sorry that I didnt make it clear.
# 20  
Old 12-08-2009
already assigned the code to you in the above threadSmilieSmilieSmilie

Code:
nawk -F"\=" '
/^[^ *$]/{b=b"|"$1 ; s=s"|"$2}
/^[ ]*$/{print b,"\n",s ; b="" ;s=""}
' file.txt | nawk -F"|" '!a[$1$2]++'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Complex Filter using grep, awk or sed

Hi, I'm not very familiar witrh sed or awk and hope the somebody can help me to solve my problem. I need to filter a text report using grep, sed or awk. I would like to cut out text lines with the pattern INFO and if exists the following lines of the pattern DETAILS. I need te keep the lines with... (4 Replies)
Discussion started by: Frankg
4 Replies

2. Shell Programming and Scripting

Complex string operation (awk, sed, other?)

I have a file that contains RewriteRules for 200 countries (2 examples for 1 country below): RewriteRule ^/at(/|/index.html|)$ http://%{HTTP_HOST}/locate/index.html?locale=de_AT #& RewriteRule ^/at_english(/|/index.html|)$ http://%{HTTP_HOST}/locate/index.html?locale=en_AT I have... (5 Replies)
Discussion started by: usshadowop
5 Replies

3. Shell Programming and Scripting

Modify sed script

I'm trying to take out strings from log files and add them to a csv. For example, in the directory now, there are 2 log files. I get the following results: sed -e '/custodian/b' -e '/packaged by/b' -e '/package name/b' -e '/Total Data (MB) Read/b' -e '/Begin Time/b' -e d * packaged by =... (10 Replies)
Discussion started by: chipperuga
10 Replies

4. Shell Programming and Scripting

A complex sed statement

I have following requirement. Say, my text file contains following patterns {2010501005|XXGpvertex|9|0|17|0|{|{30100001|XXparameter_set|@@@@{{30001002|XXparameter|!prototype_path|$AB_COMPONENTS/Sort/Sort.mpc|3|2|Pf$|@{0|}} }}@0|@315000|78500|335000|99000|114000|87000|17|And the Sort|Ab... (8 Replies)
Discussion started by: Shell_Learner
8 Replies

5. Programming

Need to modify contents of file with complex patterns.

hi, my fstab file content is like this along with some other lines: /dev/vg0/var1 /var1 ext3 defaults 0 2 /dev/vg0/flx1 /flx1 ext3 defaults 0 2 /dev/vg0/var /var ext3 defaults 0 1 /dev/vg0/flx /flx ext3 defaults 0 2 I want to remove lines with /dev/vg0/var and... (5 Replies)
Discussion started by: success
5 Replies

6. Shell Programming and Scripting

SED complex string replacement

sed -i 's:"ps -ef | grep $(cat $PID_FILE) | grep -v grep":"ps -C java -o pid,cmd | grep ${SERVER_NAME} | cut -d' ' -f1 | grep -v grep":g' scriptName That's what I'm attempting to do. I'm attempting to replace this: ps -ef | grep $(cat $PID_FILE) | grep -v grep with this: ps -C java -o... (5 Replies)
Discussion started by: cbo0485
5 Replies

7. Shell Programming and Scripting

Double Spacing complex sed pipeline

my script: FILE="$1" echo "You Entered $FILE" if ; then tmp=$(cat $FILE | sed '/./!d' | sed -n '/regex/,/regex/{/regex/d;p}'| sed -n '/---/,+2!p' | sed -n '/#/!p' | sed 's/^*//' | sed -e\ s/*:// | sed -n '/==> /!p' | sed -n '/--> /!p' | sed -n '/regex/,+1!p' | sed -n '/======/!p' | sed -n... (1 Reply)
Discussion started by: omgsomuchppl
1 Replies

8. Shell Programming and Scripting

Complex find grep or sed command

Haven't worked in bash for ages. did a good bit of shell scripting in regular sh, but have forgotten most of it. I have several thousand php files that now include the following line at the end of the file. There is no LF or CR/LF before it begins, it is just concatenated to the final line of... (3 Replies)
Discussion started by: sjburden
3 Replies

9. Shell Programming and Scripting

Complex sed replacement

Hi, I have a file that I would like to translate using sed. I can do some basic sed commands, but Im afraid this level is beyond me. I have this file - ...alter... .. ...65536... ... ...65536... ... ...alter... ... ...65536... etc What I would like to do is replace the first... (11 Replies)
Discussion started by: one_ring99
11 Replies

10. Shell Programming and Scripting

Complex Sed/Awk Question?

Hello, So i have this file called /apps/turnout which looks like that of the contents of the /etc/shadow (but not exactly) the file has a long list in it. basically, the contents of this file looks something similar to the following: jajajajalala:D#$#AFVAdfda lalabavisof:#%R@fafla#$... (3 Replies)
Discussion started by: SkySmart
3 Replies
Login or Register to Ask a Question