简体   繁体   中英

sed command line

Hi I need to run 'sed' command on file1.txt and have it extract all names that are in between StringA and StringB... ex: "Nickname":"bad_name", extract bad_name and then save all results too Output.txt. is this even possible or should I be looking at another command?

without showing more sample of your file1.txt, i am assuming you have consistent data format. If it is, then use awk

echo '"Nickname":"bad_name"' | awk -F":" '{print $NF}'

For a file, just input the file name

awk -F":" '{print $NF}' file > output.txt

Otherwise, provide more sample data for us to work with.

Using sed:

dmedvinsky@home:~$ cat file1 
"Nickname":"bad_name"
"First Name":"Dmitry"
bad line
"Dog Name":"Chuppy"
another bad line

dmedvinsky@home:~$ cat file1 | sed -e '/\(.\+\)":"\(.\+\)"/!d' -e 's/"\(.\+\)":"\(.\+\)"/\2/'
bad_name
Dmitry
Chuppy

The first expression deletes badly formatted lines, the second one replaces the pattern with 2nd matched group -- the contents of second quotes.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM