sed

November 1st, 2010 Leave a comment Go to comments

Byta ut tecken i en fil

Följande byter \\ mot / i den angivna filen:

sed -i 's#\\#/#' <path/filename>

To clean up a file you can use grep to remove all comments and sed to remove all empty lines, thanks to David Klein for this tip:

grep -v "^#" /etc/squid/squid.conf | sed -e '/^$/d'

OR, do the same with sed alone, thanks to kotnik for this tip:

cat /etc/squid/squid.conf | sed '/ *#/d; /^ *$/d'

To search for a text in a HTML file and cut out a tag:

awk -F 'link\">' '/shareTableToolbar/ {print $2}' /tmp/html_xactbull2.htm | \
cut -d "<" -f1

To clean a HTML text from the tags:

sed -e :a -e 's/<[^>]*>//g;/</N;//ba' myfile.html

Kör kommando på flera filer

for a in *.wav; do sox "$a" -t raw -r 8000 -s -w \
 -c 1 `echo $a|sed "s/.wav/.sln"` resample -ql; done

Formattera multirader

cat inputFile.txt |
 sed -e :a -e '$!N;s/\([^\\~]\)\n\([^\\~]\)/\1\\\2/;ta'
 -e 's/^\([0-9]\{1,4\}[A-Z]\{1\}\)[ ]*/\1\\C\\/' -e 'P;D' |
 sed -e :a -e 's/^\([0-9]\{1,4\}[A-Z]\{1\}[^@]*\)@/\1\\/;ta' -e 'P;D' |
 sed -e 's/^\\/@/' |
 sed -e :b -e '$!N;s/\([^~]\)\n\([^~]\)/\1\\\2/;tb' -e 'P;D' |
 sed -e '/^~/d' -e 's/\\@/@/' -e 's/@$//' |
 sed -e :a -e 's/^\([0-9]\{1,4\}[A-Z]\{1\}\)\([^@]*\)@/\1\2\n\1\\E/;ta'
 -e 'P;D' |
 sed -e :a -e 's/^\([0-9]\{1,4\}[A-Z]\{1\}\\E\\[^ ]*\)[ ]\{1,\}/\1\\/;ta'
 -e 's/,//' > outPutFile.txt

Input file:

2229A 21st Company
1 Smith Lane
@PH=294-5478
@
@FAX=103-3709
Jaketown
CA
10101
\
2970 Maloney, Jack C 329-5478@
1202 Van Patterson III, Big Mac 555-1212@
3928 Thompson, Anne Marie 395-2201
~
1248A USA Services LLC
1023 Main St
@PH=532-7900
PO Box 488
@FAX=928-9970
Big Lake
NV
19270
\
922 Hubbard Jr, Ron H 934-2890@
8685 March, Darleen B 847-7900@
~
1230A Advanced Metals
26F Maple ST-Suite 102
@PH=598-1191
@
@FAX=598-4296
Pasedena
CA
12866
\
8649 Ley, Richard 899-1191@
~

Example output:

2229A\C\21st Company\1 Smith Lane\\PH=294-5478\\\\FAX=103-3709\Jaketown\CA\10101
2229A\E\2970\Maloney\Jack\C\329-5478
2229A\E\1202\Van\Patterson\III\Big\Mac\555-1212
2229A\E\3928\Thompson\Anne\Marie\395-2201
1248A\C\USA Services LLC\1023 Main St\\PH=532-7900\PO Box 488\\FAX=928-9970\Big Lake\NV\19270
1248A\E\922\Hubbard\Jr\Ron\H\934-2890
1248A\E\8685\March\Darleen\B\847-7900
1230A\C\Advanced Metals\26F Maple ST-Suite 102\\PH=598-1191\\\\FAX=598-4296\Pasedena\CA\12866
1230A\E\8649\Ley\Richard\899-1191

Ref: http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Q_26577331.html?cid=1573#a34028298

Add prefix and postfix to lines in a file

sed script to add prefix: insert into table t1′
and suffix: ‘;

to lines in a file, example:

file1
file2
file3

for example: insert into table t1 ‘file1’;

Suppose filenames are in a file called FILE

Then

sed -e "s/^/insert into table t1 '/" -e "s/$/';/" FILE

Sortera lista och döpa om resultatfilen

Ett antal filer innehåller data strukturerat enl följande exempel:

pelle|03124|storgatan|
kalle|40193|bytorget|
lisa|31232|dorisgatan|

Filerna ska sorteras på kolumn 2 (siffrorna) och sedan sparas med nya filnamn enl:

lista1_temp.txt” –> “lista1_sort.txt

for file in /home/*_temp.txt
do
          newFileName=`echo $file | sed 's/temp/sort/'`
          sort -t'|' -k 2 $file > $newFileName
done;

Ref: http://www.experts-exchange.com/OS/Linux/Distributions/Debian/Q_26278043.html?cid=1573#a33057622

  1. No comments yet.
  1. No trackbacks yet.