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?
+2
A:
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.
ghostdog74
2010-07-24 04:56:49
A:
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.
d.m
2010-07-24 05:13:31
the file has other user information in it so i have to isolate "Nickname":" (with the quotations) and extract bad_name with the ending string of ",ex: "Nickname":"bad_name",output: bad_namethe : can't be used by itself to determine where the name starts only because its not unique in the other data sets us the : as well
acctman
2010-07-24 06:54:21
Well, in that case you need to change the first part of the expression (first subpattern) to `"Nickname"`.So the command will look like:`cat file1 | sed -e '/"Nickname":"\\(.\+\\)"/!d' -e 's/"Nickname":"\\(.\+\\)"/\1/'`
d.m
2010-07-24 08:10:26
I tried the command you list and it just displayed the file content. the sed command didn't run or output anything. cat file1.txt | sed -e '/"Nickname":"\(.\+\)"/!d' -e 's/"Nickname":"\(.\+\)"/\1/'
acctman
2010-07-24 09:34:32
Yep, my bad. Try this:`cat file1 | sed -e '/"Nickname":"\\([^"]\+\\)"/!d' -e 's/.*"Nickname":"\\([^"]\+\\)".*/\1/'`To output to file just use ` > output`.
d.m
2010-07-24 21:22:46