Pages

Affichage des articles dont le libellé est sed. Afficher tous les articles
Affichage des articles dont le libellé est sed. Afficher tous les articles

vendredi 30 novembre 2012

insert text after one line which contains patterns

In a file I would like to insert a line after the line which contains "MRINI".

1) firstly, find the line number which contains the pattern
 set line_number =  `sed  -n  '/MRINI/='   filename `
-n : to only print the line number, not print the pattern space

2) use sed to insert a line after the line_number. The line to insert is:
    1 7840    0    0    0    0    0    0               fin correction mesures

The command is:   sed -e "${line_number}a \line_to_insert"   filename

sed   -e   "${line_number}a\    1 7840    0    0    0    0    0    0               fin correction mesures"       filename   > new_filename


Note: 
i) the double quote is used in 2) since the variable ${line_number} is used in the command.

ii) In the command sed -e "${line_number}a \line_to_insert"   filename ,
a\line_to_insert represents that we append the line_to_insert 
If use i\line_to_insert: insert the line_to_insert in the $line_number, ie., before the line which contains the pattern MRINI

iii) in 1) we use   '/MRINI/='  
= represents that we print the current line number where it contains the pattern MRINI




lundi 22 octobre 2012

add characters in the beginning/end of each line


---- add "\hline" in the begining of each line in a file one_year_stat
sed 's/^/ \\hline /' one_year_stat  > tt

---- add "\" in the end of each line in a file:
sed 's/$/ \\/'    tt 


---- add " &" in the end of each line in a file:
sed 's/$/  \&/'   tt

---- replace the last occurence of & in each line of a file with a space:

sed 's /\(.*\)& /\1 /' one_year_stat


-----If the new text contains "/", eg a path /home/user/tt, then the sed 's/../g' does not work. We should use "|" instead of "/"
set aa = /home/user/tt
sed   "s|^|${aa}|g"    txt_file 


NOTE: For special characters such as \ or &;, if we want to add then in the text file using the sed. We need put \ before them such that they may be added in the file.

lundi 18 juin 2012

sed to replace a string in a file and save it

#!/bin/bash

for i in $(ls -d p*); do
    echo $i
    cd $i
    echo $PWD
    grep "elseif (( hrut .gt. 29.2)" *
    sed -i 's/(( hrut .gt. 29.2) then/( hrut .gt. 29.2) then/g'  file.f;   
   cd ..

done

sed -i  's/string1/string2/g'  file:
it can replace the string1 with string 2 in the file, and save the modified file with the original name.


____ to replace strings at certain lines:
set a = 50
set b = 60
sed -e "$a,${b}s/string1/string2/g"  file

This is to replace the string1 with string2 at lines between 50 and 60 in a file

mardi 22 mai 2012

sed: remove text between two keywords

If the inputfile contains:

la1_22000
la1_22066
la1_22124
la1_22236
la1_22341
la1_22431
la1_22572

In order to remove the text between two lines which contains the keywords la1_22066 and la1_22431 respectively,

set keyword1 = la1_22066
set keyword2 = la1_22431
sed "s/$keyword1/,/$keyword2/d"    inputfile


Attention: in order to use variables in SED, it is better to use double quote than single quote.


If we have special characters in the keyword, for example, /, what we can do is to add \ before it, such that the system can identify this special character.

Example: to repalce /aaa with /bbb
set 's/\/aaa/\/bbb/g' inputfile

lundi 2 avril 2012

get text of between specific lines using sed

1) sed -n 'linenumber1,linenumber2p'  file
sed -n '10, 20p' file


Similarly, if you want to print from 10 to the end of line you can use: sed -n '10,$p' filename
This is especially useful if you are dealing with a large file. Sometimes you just want to extract a sample without opening the entire file.

If use variables to define the line number, then we need use double quote and {}:
set a = 1
set b = 5
sed -n "$a, ${b}p" stations_igs_final


2) sed -n 5p  file

or use awk:

cat file | awk -v n=5 '{if (NR==n)print $0}'

vendredi 16 mars 2012

find and change in text with grep and sed

given a text file ($file) containing:


1.0  1  user  /home/user/tahiti_la2_mrb0/la2_22530_elim_1fois
1.0  1  user  /home/user/tahiti_la2_mrb0/la2_22540_elim_1fois
1.0  1  user  /home/user/tahiti_sta_mrb0/sta_22460_elim_1fois
1.0  1  user  /home/user/tahiti_sta_mrb0/sta_22470_elim_1fois


if we want to change the 1.0 to 0.6 in any line which contains "la", and change 1.0 to 1.2 in any line which contains "st", we can do:


cat  $file  |  grep "_la"   | sed   's/1./0.6/'     >> $file"_0.61.2"
cat  $file  |  grep "_st"   | sed   's/1./1.2/'     >> $file"_0.61.2"



mardi 13 mars 2012

sed regular expression

^matches the beginning of the line
$matches the end of the line
.Matches any single character
(character)*match arbitrarily many occurences of (character)
(character)?Match 0 or 1 instance of (character)
[abcdef]
Match any character enclosed in [] (in this instance, a b c d e or f) ranges of characters such as [a-z] are permitted. The behaviour of this deserves more description. See the page on grep for more details about the syntax of lists.
[^abcdef]
Match any character NOT enclosed in [] (in this instance, any character other than a b c d e or f)
(character)\{m,n\}Match m-n repetitions of (character)
(character)\{m,\}Match m or more repetitions of (character)
(character)\{,n\}Match n or less (possibly 0) repetitions of (character)
(character)\{n\}Match exactly n repetitions of (character)
\(expression\)Group operator.
\nBackreference - matches nth group
expression1\|expression2Matches expression1 or expression 2. Works with GNU sed, but this feature might not work with other forms of sed.



A very useful link about sed is:
 http://www.ibm.com/developerworks/linux/library/l-sed2/index.html

add in the end of line, sed

1) in sed, "^" represents the begining of a line, and "$" the end of line, "^$" a blank line

2) if we want to add a string in the end of each line of a text file and save it to a new file:


#!/bin/csh
# find each file starting with "input" and apply the changes
foreach i ( `ls input*` )
    # get the last string in the filename separated by _
    set append = `echo $i | awk '{split($0,a, "_");print a[6]}' `     


    # add _elim_1fois at the end of each line and save it to a new file
    sed    's/$/_elim_1fois/'   $i  >> $i"_elim_1fois_"$append 
   
    sed   's/$/_elim_2fois/'    $i  >> $i"_elim_2fois_"$append


end

lundi 25 avril 2011

replace a line in a text file with sed

I have a text file which defines the values of some important variables. Such a text file is used for several simulations. I need to change the value of one parameter in the file:
ap = 24.1, to a smaller value 3.

what I do is to use the sed.
sed 's/to_be_replace/replaced_result/' text file

mv text_file tt
sed 's/ap = 24.1,/ap = 3,/p' tt > text_file

then the old text file is modified with the ap value !