Sed (POSIX) coming from linux [duplicate] - sed

This question already has answers here:
How to remove every other line with sed?
(5 answers)
Closed 8 years ago.
I am somewhat new to posix, and i can't use: sed '1~2p'
My goal is to skip every one line from line 1:
1
2
3
4
would become
1
3
I was wondering what is the posix equivalent of ~.

Code for sed:
sed -e n -e d file
or:
sed -e 'n;d' file

The simpler, portable solution would be:
awk 'NR%2' file

bash solution:
while read -r line; do
[ $((i++ % 2)) -eq 0 ] && echo "$line";
done < file

Related

How to find pattern from route string and edit next line in bash shell? [duplicate]

This question already has answers here:
find matching text and replace next line
(3 answers)
Closed 4 years ago.
cat test.txt
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
Above is target file test.txt.I need to find pattern baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/,replace next line enabled=0 to enabled=1.
I tried sed '#baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/#!b;n;cenabled=1' test.txt but failed.
Notice:CANNOT use other delimeter such as # instead of / because
it's not replacement command.
Thanks in advance!
If you are ok with awk could you please try following.
awk '
/baseurl=http:\/\/repo\.mysql\.com\/yum\/mysql-5\.5-community\/el\/7\/\$basearch\//{
print
flag=1
next
}
flag && /enabled/{
print "enabled=1"
flag=""
next
}
1
' Input_file
In case you want to save output into Input_file itself append > temp_file && mv temp_file Input_file in above code.
This might work out—it finds 'baseurl=' then grabs the next line and substitutes 'enable=0' with 'enabled=1':
sed '/baseurl=/ {N;s/enabled=0/enabled=1/;}' test.txt
Feel free to change the initial regex to the line you mentioned. I just wanted to show the general solution.
I hope this helps!
sed -E '/baseurl=http:\/\/repo\.mysql\.com\/yum\/mysql-5\.5-community\/el\/7\/\$basearch\//!b;n;cenabled=1' test.txt > temp_file && mv temp_file test.txt
This answer come from RavinderSingh13 and WiktorStribiżew,I just comibne it.

sed: -e expression #1, char 5: unterminated `s' command [duplicate]

This question already has answers here:
sed edit file in place
(15 answers)
Closed 4 years ago.
I'm learning SED and I saw this sed substitution example. It's supposed to replace the first lowercase t as uppercase in each new line.:
$ sed 's/t/T' text-01.txt
sed: -e expression #1, char 5: unterminated `s' command
Contents of file:
$ cat text-01.txt
10 tiny toes
this is that
5 funny 0
one two three
tree twice
It's not the end of the world though, since I can just output into a new file:
cat text-01.txt | sed 's/t/T/' > text-02.txt
But what am I supposed to do if I want to edit the original file?
The commands aren't the same, the closing / is missing in the first one:
# v
sed 's/t/T' text-01.txt
cat text-01.txt | sed 's/t/T/' > text-02.txt
# ^

Delete lines by pattern in specific range of lines

I want to remove lines from file by regex pattern using sed just like in this question Delete lines in a text file that containing a specific string, but only inside a range of lines (not in the whole file). I want to do it starting from some line number till the end of file.
This is how I've done it in combination with tail:
tail -n +731 file|sed '/some_pattern/d' >> file
manually remove edited range in file from previous step
Is there a shorter way to do it with sed only?
Something like sed -i '731,1000/some_pattern/d' file?
You can use this sed,
sed -i.bak '731,1000{/some_pattern/d}' yourfile
Test:
$ cat a
1
2
3
13
23
4
5
$ sed '2,4{/3/d}' a
1
2
23
4
5
You need $ address to match end of file. With GNU sed:
sed -i '731,${/some_pattern/d;}' file
Note that this can be slower than tail -n +number, because sed will start processing at start of file instead of doing lseek() like tail.
(With BSD sed you need sed -i '' ...)
sed is for simple substitutions on individual lines, that is all. For anything even marginally more interesting an awk solution will be clearer, more robust, portable, maintainable, extensible and better in just about ever other desirable attribute of software.
Given this sample input file:
$ cat file
1
2
3
4
1
2
3
4
1
2
3
4
The following script will print every line except a line containing the number 3 that occurs after the 6th line of the input file:
$ awk '!(NR>6 && /3/)' file
1
2
3
4
1
2
4
1
2
4
Want to only do the deletion between lines 6 and 10? No problem:
$ awk '!(NR>6 && NR<10 && /3/)' file
1
2
3
4
1
2
4
1
2
3
4
Want the skipped lines written to a log file? No problem:
awk 'NR>6 && /3/{print > "log";next} {print}' file
Written to stderr?
awk 'NR>6 && /3/{print | "cat>&2";next} {print}' file
Want a count of how many lines you deleted also written to stderr?
awk 'NR>6 && /3/{print | "cat>&2"; cnt++; next} {print} END{print cnt | "cat>&2"}' file
ANYTHING you want to do additionally or differently will be easy and build on what you start with. Try doing any of the above, or just about anything else, with a sed script that satisfies your original requirement.
awk to the rescue!
awk '!(NR>=731 && /pattern/)' input > output

Substitution option: s in sed command [duplicate]

This question already has answers here:
How to use sed to replace only the first occurrence in a file?
(25 answers)
Closed 8 years ago.
I am using sed command make substitution in the file.
Assume my file1 is:
10
11
10
11
Then I want to substitute the 1st "10" in file1 to "12", and dump to file2. The file2 should be:
12
11
10
11
I tried this command:
sed 's/10/12/' file1 >file2
But it changed the 2nd "10" also. So, how can I write the sed command to do that?
If you can use awk instead of sed, you can have more control like this:
awk '!changed && /10/ { sub(/10/, "12"); changed = 1}1' file1
12
11
10
11
try:
sed '0,/10/s/10/12/' file1 >file2
Like GriffinG said, you can do something like this:
sed '0,/10/ s/10/12/' file1 > file2
The 0,/10/ at the beginning sets the bounds on the following substitution command. It says start from line 0 (the first line), and go until a line it matches the pattern /10/. So After that first match, sed will stop processing that substitution.
If you do not have GNU sed, try:
echo | cat - file | sed '1,/10/s/10/12/; 1d'
or
sed '1,/10/s/10/12/; 1d' << EOF
$(cat file)
EOF
or in bash / ksh93
sed '1,/10/s/10/12/; 1d' <(echo; cat file)
GNU sed knows 0,/10/ so the extra empty line is not required

strip the last and first character from a String

Is fairly easy to strip the first and last character from a string using awk/sed?
Say I have this string
( 1 2 3 4 5 6 7 )
I would like to strip parentheses from it.
How should I do this?
sed way
$ echo '( 1 2 3 4 5 6 7 )' | sed 's/^.\(.*\).$/\1/'
1 2 3 4 5 6 7
awk way
$ echo '( 1 2 3 4 5 6 7 )' | awk '{print substr($0, 2, length($0) - 2)}'
1 2 3 4 5 6 7
POSIX sh way
$ var='( 1 2 3 4 5 6 7 )'; var="${var#?}"; var="${var%?}"; echo "$var"
1 2 3 4 5 6 7
bash way
$ var='( 1 2 3 4 5 6 7 )'; echo "${var:1: -1}"
1 2 3 4 5 6 7
If you use bash then use the bash way.
If not, prefer the posix-sh way. It is faster than loading sed or awk.
Other than that, you may also be doing other text processing, that you can combine with this, so depending on the rest of the script you may benefit using sed or awk in the end.
why doesn't this work? sed '..' s_res.temp > s_res.temp ?
This does not work, as the redirection > will truncate the file before it is read.
To solve this you have some choices:
what you really want to do is edit the file. sed is a stream editor not a file editor.
ed though, is a file editor (the standard one too!). So, use ed:
$ printf '%s\n' "%s/^.\(.*\).$/\1/" "." "wq" | ed s_res.temp
use a temporary file, and then mv it to replace the old one.
$ sed 's/^.\(.*\).$/\1/' s_res.temp > s_res.temp.temp
$ mv s_res.temp.temp s_res.temp
use -i option of sed. This only works with GNU-sed, as -i is not POSIX and GNU-only:
$ sed -i 's/^.\(.*\).$/\1/' s_res.temp
abuse the shell (not recommended really):
$ (rm test; sed 's/XXX/printf/' > test) < test
On Mac OS X (latest version 10.12 - Sierra) bash is stuck to version 3.2.57 which is quite old. One can always install bash using brew and get version 4.x which includes the substitutions needed for the above to work.
There is a collection of bash versions and respective changes, compiled on the bash-hackers wiki
To remove the first and last characters from a given string, I like this sed:
sed -e 's/^.//' -e 's/.$//'
# ^^ ^^
# first char last char
See an example:
sed -e 's/^.//' -e 's/.$//' <<< "(1 2 3 4 5 6 7)"
1 2 3 4 5 6 7
And also a perl way:
perl -pe 's/^.|.$//g'
If I want to remove the First (1) character and the last two (2) characters using sed.
Input "t2.large",
Output t2.large
sed -e 's/^.//' -e 's/..$//'
`