Saturday 6 April 2013

Strings or File Manipulation

How to replace tab with newline:

sys@umlinux:~$ cat list
str1    str2    str3    str4    str5    str6    str7    str8
sys@umlinux:~$ cat list | tr '\t' '\n' >list2
sys@umlinux:~$ cat list2
str1
str2
str3
str4
str5
str6
str7
str8

How to get different parts of a string, for example with path and file name:

sys@umlinux# foo=/tmp/dir/filename.tar.gz
sys@umlinux# echo ${foo%/*}
/tmp/dir
sys@umlinux# echo ${foo##*/}
filename.tar.gz
sys@umlinux# echo ${foo%%.*}
/tmp/dir/filename
sys@umlinux# echo ${foo%.tar.gz}
/tmp/dir/filename
sys@umlinux# echo ${foo#*.}
tar.gz

How to find and rename recursively

First example is for files and second for directories:
sys@umlinux# for FILE in $(find . -type f -name "*foo*"); do NEW=`echo $FILE | sed -e 's/foo/bar/'`; mv "$FILE" "$NEW"; done
sys@umlinux# for DIR in $(find . -type d -name "*foo*"); do NEW=`echo $DIR | sed -e 's/foo/bar/'`; mv "$DIR" "$NEW"; done

How to display with awk from a desired column to the end

In this example, we will display the free space for all vgs in one AIX system. This line should work in all systems with the necessary modifications, up to your needs and system's specifications:
root@um:~# for i in $(lsvg -o); do printf "$i ==>\t";lsvg $i| grep -i free| awk '{ print substr($0, index($0,$4)) }'; done
datavg06 ==>    FREE PPs:       3 (384 megabytes)
datavg ==>      FREE PPs:       489 (62592 megabytes)
datavg01 ==>    FREE PPs:       549 (70272 megabytes)
datavg04 ==>    FREE PPs:       4 (512 megabytes)
datavg03 ==>    FREE PPs:       109 (13952 megabytes)
datavg02 ==>    FREE PPs:       159 (20352 megabytes)
datavg07 ==>    FREE PPs:       4 (256 megabytes)
datavg08 ==>    FREE PPs:       125 (8000 megabytes)
datavg05 ==>    FREE PPs:       143 (18304 megabytes)
rootvg ==>      FREE PPs:       13 (1664 megabytes)

How to search for a string in multiple files on linux:

root@linux:~# find . -type f -print0 | xargs -0 grep -i book

How to search and replace a string in multiple files on linux:

root@linux:~# find . -type f -print0 | xargs -0 sed -i 's/book/BOOK/g'

How to display the last column from an output:

root@aix:/ # lssrc -s nimsh
Subsystem         Group            PID          Status
 nimsh            nimclient                     inoperative
root@aix:/ # lssrc -s nimsh | awk '{print $NF}'
Status
inoperative

root@aix:/ # lssrc -s nimsh
Subsystem         Group            PID          Status
 nimsh            nimclient        954570       active
root@aix:/ # lssrc -s nimsh | awk '{print $NF}'
Status
active

Passing variable to awk:

root@linux:~# for i in 21684 26667 20991; do ps -ef | awk -v ss="$i" '$2 ~ ss { print }'; done
root     21684 10157  0 Nov22 tty1     00:00:00 -bash
root     26667     1  0 Nov22 ?        00:00:00 dhclient3 eth2
vbox     20991 20983  0 Oct24 ?        00:00:00 gnome-session
another example:
florian@florian:~$ for i in 1 2 3 4 5; do echo $i | awk -v ss="awkvar$i" '{print ss" from "$0}'; done
awkvar1 from 1
awkvar2 from 2
awkvar3 from 3
awkvar4 from 4
awkvar5 from 5

How to count a string's characters:

root@linux:~$ STRING="12345"
root@linux:~$ echo "Chars: ${#STRING}"
Chars: 5

How to remove blank lines from a file using grep and awk:

root@linux:~# cat /tmp/test.txt
1234567890

abcde
fgh

ijklmn
root@linux:~# grep -v ^$ /tmp/test.txt
1234567890
abcde
fgh
ijklmn
root@linux:~# awk 'NF != 0 {print} ' /tmp/test.txt
1234567890
abcde
fgh
ijklmn

How to add a line after some string

(to add "BOOOOOOO!" if the line contains "abc" (first example) or if the line is "fgh" (second example)):
root@linux:~# awk '{print ; if ($0 ~ "abc") print "BOOOOOOO!"}' /tmp/test.txt
1234567890
abcde
BOOOOOOO!
fgh
ijklmn
root@linux:~# awk '{print ; if ($0 == "fgh") print "BOOOOOOO!"}' /tmp/test.txt
1234567890
abcde
fgh
BOOOOOOO!
ijklmn

How to transform a line from lowercase to uppercase with sed:

root@linux:~# sed 's/abcde/ABCDE/g' /tmp/test.txt
1234567890
ABCDE
fgh
ijklmn

How to split a string after the second delimiter with sed:

system@umlinux:~$ echo "1:2:3:4:5:6:7:8:9" | sed "s/\([^:]*:[^:]*\):/\1\n/g"
1:2
3:4
5:6
7:8
9
or
system@umlinux:~$ echo "1:2:3:4:5:6:7:8:9" | sed "s/[^:]*:[^:]*:/& /g" | sed "s/: /\n/g" 
1:2
3:4
5:6
7:8
9
or in ksh
gz@aix~# echo "1:2:3:4:5:6:7:8:9" | sed "s/\([^:]*:[^:]*\):/\1 /g" | tr " " "\n"
1:2
3:4
5:6
7:8
9

How to replace the 3rd same (repetitive) character with another:

srt@gz$ echo "00:21:26:400 --> 00:21:28:400" | sed "s/\([^00:]*:[^:]*:[^:]*\):/\1\,/g"
00:21:26,400 --> 00:21:28,400

How to convert uppercase to lowercase characters in a file with TR:

florian@florian:~$ cat b
ASLDJSLDJSLKJD
SLKJDLKSJDLK
SOIDUSOIDUOIS
SDOSDSJLD
florian@florian:~$ cat b | tr '[:upper:]' '[:lower:]' 
asldjsldjslkjd
slkjdlksjdlk
soidusoiduois
sdosdsjld

0 blogger-disqus:

Post a Comment