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
# 8  
Old 12-05-2009
Quote:
Originally Posted by binlib
Sed is Turing-complete. You can do everything in sed.

Code:
sed '
s/^[^|]*|//
N
:o
s/\([^|]*\)|\(.*\n\)\([^|]*\)|/\1=\3\n\2/
P
s/^[^\n]*\n//
to


That example demonstrates why awk is more suitable for anything more than simple selection and replacement.
# 9  
Old 12-07-2009
Data Please help me more with it

Hello,

Thank you for the code. Now i need more of it (sorry I am really new with awk)

I need 2 different scripts which can rearrange the data like this:

xy=CreateDB|head.queue|head.source|head.definition|rtf.edit
abc|source|divine|line4
def|source||line2

head.queue=abc
head.source=source
head.definition=divine
rtf.edit=line4

head.queue=def
head.source=source
head.definition=
rtf.edit=line2

quirkasaurus has helped me with this, but how can I add a blank line between different set of data value?

I also need a reverse of this, from:

head.queue=abc
head.source=source
head.definition=divine
rtf.edit=line4

head.queue=def
head.source=source
head.definition=
rtf.edit=line2

to:

xy=CreateDB|head.queue|head.source|head.definition|rtf.edit
abc|source|divine|line4
def|source||line2

Thank you for your help!
# 10  
Old 12-07-2009
To add extra blank line just add the below bold red line to the same old nawk:-


Code:
nawk -F\| '{
  if ( NR == 1 ){
    for ( x = 2; x <= NF; x++ ){
      headers[x-1]=$x;
      }
    next;
    }

  for ( x = 1; x <= NF; x++ ){
    print headers[x] "=" $x;
    }
print ""
  }'

to reverse the action use the below nawk:-

Code:
nawk -F"\=" '
/head.queue/{c=5 ; b="xy=CreateDB""|"$1 ; s=$2 ; next}
c--{b=b"|"$1 ; s=s"|"$2}
!c--{print b,"\n",s ; b="" ;s=""}
' file.txt | nawk '!a[$0]++'

SmilieSmilieSmilie
# 11  
Old 12-07-2009
Hello Ahmad,

Thanks for the help, but the secondScript not really working properly.

When converted the Input file to:

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

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

After executing your script, it gives me back :
xy=CreateDB|head.queue|head.source|head.definition|rtf.edit|rtf.task
abc|source|divine|line4
def|source|divine|line3

Not all the data were reversed and there is a space in the front of the data set.

Can you help out?

Thank you in advance
# 12  
Old 12-07-2009
Others have given the solution. My question is,

why put the file name xy=CreateDB in first line, can it be seperated into a single line? With that, the code can be easier and more Efficient.
# 13  
Old 12-08-2009
Hello rdcwayx

I do need the same format as I posted. The xy=CreateDB has to be in the same line with the |head.queue|head.source|head.definition|....

Can anyone help out?
# 14  
Old 12-08-2009
Quote:
Originally Posted by pinkypunky
Hello Ahmad,

Thanks for the help, but the secondScript not really working properly.

When converted the Input file to:

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

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

After executing your script, it gives me back :
xy=CreateDB|head.queue|head.source|head.definition|rtf.edit|rtf.task
abc|source|divine|line4
def|source|divine|line3

Not all the data were reversed and there is a space in the front of the data set.

Can you help out?

Thank you in advance
the first thread you put there were only 4 rows but now you are having 8 rows
kindly modify my code as below:-

Code:
nawk -F"\=" '
/head.queue/{c=8 ; b="xy=CreateDB""|"$1 ; s=$2 ; next}
--c{b=b"|"$1 ; s=s"|"$2}
!c{print b,"\n",s ; b="" ;s=""}
' file.txt | nawk '!a[$0]++'

where c= number of raws in each paragraph


Last edited by ahmad.diab; 12-08-2009 at 07:27 AM..
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