开发者

sed command line

开发者 https://www.devze.com 2023-01-09 04:26 出处:网络
Hi I need to run \'sed\' command on file1.txt and have it extract all names that are in between StringA and StringB.开发者_如何学C.. ex: \"Nickname\":\"bad_name\", extract bad_name and then save all r

Hi I need to run 'sed' command on file1.txt and have it extract all names that are in between StringA and StringB.开发者_如何学C.. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消