Replace a char in a string
echo $SN | sed 's/-/_/g'Extract a column
[root@mail slaves]# ll -tr | grep -v total-rw------- 1 root root 18196 Mar 1 17:22 example.com.hosts
-rw-r--r-- 1 root root 172 Mar 6 16:55 example.com.hosts.bak
-rw------- 1 named named 358 Mar 11 08:34 sec.example.com
[root@mail slaves]# ll -tr | grep -v total | awk -F" " '{print $9}'
example.com.hosts
example.com.hosts.bak
sec.example.com
Replace a char every token
[root@mail filelist]# lsf_1363024548 f_1363024566 f_1363024569 f_1363024572
f_1363024564 f_1363024567 f_1363024570 f_1363024573
f_1363024565 f_1363024568 f_1363024571
[root@mail filelist]# for f in `ls`; do
> echo $f | sed 's/f/a/g'
> done
a_1363024548
a_1363024564
a_1363024565
a_1363024566
a_1363024567
a_1363024568
a_1363024569
a_1363024570
a_1363024571
a_1363024572
a_1363024573
Replace char(s) in a file
[root@mail filelist]# cat file.txt | sed 's/ffff/1/g'abcd1abcd1
efgh1eeef
Print strings in specified format
[root@mail filelist]# printf "%s %s\n" "abc" "def"abc def
Replace a space in a string to a return "\n"
[root@localhost ~]# str="abc def ijk"[root@localhost ~]# echo $str
abc def ijk
[root@localhost ~]# echo $str | sed 's/\s/\n/g'
abc
def
ijk
Extract Token from a string
note that the regex is using greedy match, if we use gender=\(.*\), it will map till the place before school="ics"e.g str="name=nana gender=girl age=5 school=ics"
sed -n 's/.*[[:blank:]]gender=\([a-z]*\)[[:blank:]].*/\1/p' $str
I want to grab the last two numbers (one int, one float; followed by optional white space) and print only them.
ReplyDeleteExample:
foo bar bla 1 2 3.4
Should print:
2 3.4
Match the whole line, so add a .* at the beginning of your regex. This causes the entire line to be replaced with the contents of the group
echo "foo bar bla 1 2 3.4" |
sed -n 's/.*\([0-9][0-9]*[\ \t][0-9.]*[ \t]*$\)/\1/p'
2 3.4
cat file.txt
ReplyDelete0 NULL 1729720826 24130 10 14198703 21804491
0 NULL 1729720826 19214 20 16156334 21332425
0 NULL 1729720826 27574 30 11274774 17520328
awk -F" " '{x += $5} END {print "Sum: "x}' file.txt
>> Sum: 60