advertisements
_____________________________________________________________________________________________________________________
I
have a text file called dat.txt and it having some generic text characters in
the first and last of every line. I wanted to remove these charcters. See
different options by using vi, awk, sed.
Content
of my input dat.txt file is
$ cat dat.txt
xml.1-CCJGL1-CCJGL1-CCJGL.rec
xml.1-BSDF0Q1-BW;LKJ1-BWP30Q.rec
xml.1-LKJ<MN1-1LKJMG1-1W13LG.rec
xml.1-2<MBMV1-NVNBVKJH21HMRE.rec
xml.1-2EW*&Y1-(878761-2AJKGY.rec
Using vi editor:
Open
the file in vi and run following commands.
- To remove 4 characters from the beginning
of every lines of a file
:%s/^....//g
- To remove 4 characters from the end of
every lines of a file
:%s/....$//g
Using sed
- Remove 4 characters from the beginning
of every lines
$ sed -r "s/^(.{0})(.{4})//" dat.txt
1-CCJGL1-CCJGL1-CCJGL.rec
1-BSDF0Q1-BW;LKJ1-BWP30Q.rec
1-LKJ<MN1-1LKJMG1-1W13LG.rec
1-2<MBMV1-NVNBVKJH21HMRE.rec
1-2EW*&Y1-(878761-2AJKGY.rec
- To remove 4 characters from the end of
every lines of a file
$ sed -r "s/(.{0})(.{4})$//" dat.txt
xml.1-CCJGL1-CCJGL1-CCJGL
xml.1-BSDF0Q1-BW;LKJ1-BWP30Q
xml.1-LKJ<MN1-1LKJMG1-1W13LG
xml.1-2<MBMV1-NVNBVKJH21HMRE
xml.1-2EW*&Y1-(878761-2AJKGY
Using awk
- Remove 4 characters from the beginning
of every lines
$ awk 'sub("^....", "")'
dat.txt
1-CCJGL1-CCJGL1-CCJGL.rec
1-BSDF0Q1-BW;LKJ1-BWP30Q.rec
1-LKJ<MN1-1LKJMG1-1W13LG.rec
1-2<MBMV1-NVNBVKJH21HMRE.rec
1-2EW*&Y1-(878761-2AJKGY.rec
- Remove 4 characters from the end of
every lines
$ awk 'sub("....$", "")'
dat.txt
xml.1-CCJGL1-CCJGL1-CCJGL
xml.1-BSDF0Q1-BW;LKJ1-BWP30Q
xml.1-LKJ<MN1-1LKJMG1-1W13LG
xml.1-2<MBMV1-NVNBVKJH21HMRE
xml.1-2EW*&Y1-(878761-2AJKGY
Using Shell Script
- Remove 4 characters from the beginning
of every lines
$ while read LINE
do
echo "$LINE"|cut -c5-
done
< dat.txt
$ while read LINE
do
echo ${LINE:4}
done
< dat.txt
- Remove 4 characters from the end of
every lines
$ while read LINE
do
echo "${LINE%????}"
done < dat.txt_____________________________________________________________________________________________________________________
0 comments:
Post a Comment