Linux/sed

From Omnia
Revision as of 22:30, 25 September 2018 by Kenneth (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Delete empty lines, and search lines:

$ sed '/^$/d' /tmp/data.txt
$ sed '/^$/d' /tmp/data.txt > /tmp/output.txt
$ sed '/Windows/d' /tmp/data.txt > /tmp/output.data.txt

Multiple:

echo "ac" | sed 's/a/b/ ; s/c/d/'

In place edit:

sed -i 's/old/new' myfile.txt

Change found text:

's/{old value}/{new value}/g'
sed 's/some_text/new_text/' myfile.txt
sed 's/string1/string2/g'	# Replace string1 with string2
sed 's/\(.*\)1/\12/g'		# Modify anystring1 to anystring2
sed '/ *#/d; /^ *$/d'		# Remove comments and blank lines
sed ':a; /\\$/N; s/\\\n//; ta'	# Concatenate lines with trailing \
sed 's/[ \t]*$//'		# Remove trailing spaces from lines
sed 's/\([\\`\\"$\\\\]\)/\\\1/g'		# Escape shell metacharacters active within double quotes
seq 10 | sed "s/^/      /; s/ *\(.\{7,\}\)/\1/"	# Right align numbers
sed -n '1000p;<acronym title="quit ASAP">1000q</acronym>'	# Print 1000th line
sed -n '10,20p;<acronym title="quit ASAP">20q</acronym>'	# Print lines 10 to 20
# Extract title from HTML web page
sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;<acronym title="quit after match">T;q</acronym>'	

Remove last occurrence:

echo "/dir/" | rev | sed 's#/##' | rev    # /dir
rev inputfile | sed 's/ /-/' | rev

Change ASCII 00 character: (using octal value) [1]

sed -i "s/\o00/ /g" filename  # change 00 to space
sed -i "s/\o00/\\n /g" filename  # change 00 to new line

Change copyrights in several files:

sed -i.BAK -e '/Copyright.*Fusion/s/-20[0-9][0-9]//;/Copyright.*Fusion/s/\(20[0-9][0-9]\)/\1-2013/' `ls *.c`

References:

sed one-liners

[http://www.catonmat.net/blog/wp-content/uploads/2008/09/sed1line.txt sed one-liners (.txt)

Crazy

Remove the first two numbers from itunes songs: ("02 Rolling in the Deep.mp3" -> "Rolling in the Deep.mp3")

ls | awk '{print "mv \"" $0 "\" \"" $0 "\""}' | grep "^mv \"\([0-9]\+\)" | sed 's/"[0-9]\{1,2\} /"/2' | sh

Convert Line Ending

sed 's/^M$//' windows.txt > unix.txt
sed 's/\r$//' windows.txt > unix.txt