I'm attempting to remove a certain pattern from a line, but not the entire line itself. An example would be:
Original:
user=dannyBoy
Desired:
dannyBoy
I have a file that is full of lines like that, so I was wondering how I would be able to cut a specific part of the text off, whether that be just removing the first five characters from the list or searching for the pattern "user=" and removing it.
There are many ways to do this:
cut -d'=' -f2- file
sed 's/^[^=]*//' file
awk -F= '{print $2}' file #if just one = is present
cut
sets a delimiter ( -d'=
) and then prints all the fields starting from the 2nd one ( -f2-
). sed
looks for all the content from the beginning up to the first =
and removes it. awk
sets =
as field separator and prints the second field. The command below deletes the first 5 characters:
$ echo "user=dannyboy" | cut -c 6-
You can use it on a file with cut -c 6- inputfilename
as well.
Using ex
:
echo user=dannyBoy | ex -s +"norm df=" +%p -cq! /dev/stdin
where ex
is equivalent to vi -e
/ vim -e
which basically executes vi command: df=
(delete until finds =
), then print the buffer ( %p
).
If you've multiple lines like that, then it would be simpler by using substitution:
ex -s +"%s/^.*=//g" +%p -cq! foo.txt
To edit file in place, change -cq!
to -cwq
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.