[SOLVED] sed or awk help - need to remove text on each line before a regular expression
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Could anyone help me set up a script to make this happen? It seems like sed and awk could both do this easily but I can't figure out the syntax to tell either to remove all text except the > before the |LGIG|.
This could be done in one line too with sed or probably with sed/awk combination But my knowledge is limited to sed/awk.
/tmp/file is your base file and /tmp/file1 is what you need.
Code:
for i in `cat /tmp/file`
do
echo "$i" | grep "^>"
if [ $? -eq 0 ]
then
echo "$i" | awk -F"|" '{print $3}' >> /tmp/file1
else
echo "$i" >> /tmp/file1
fi
done;
OR a one liner
Code:
for i in `cat /tmp/file`; do echo "$i" | grep "^>"; if [ $? -eq 0 ]; then echo "$i" | awk -F"|" '{print $3}' >> /tmp/file1; else echo "$i" >> /tmp/file1; fi; done;
Hope this helps.
Last edited by vikas027; 10-28-2009 at 06:37 PM.
Reason: Need to print $3 in awk
This could be done in one line too with sed or probably with sed/awk combination But my knowledge is limited to sed/awk.
/tmp/file is your base file and /tmp/file1 is what you need.
Code:
for i in `cat /tmp/file`
do
echo "$i" | grep "^>"
if [ $? -eq 0 ]
then
echo "$i" | awk -F"|" '{print $3}' >> /tmp/file1
else
echo "$i" >> /tmp/file1
fi
done;
OR a one liner
Code:
for i in `cat /tmp/file`; do echo "$i" | grep "^>"; if [ $? -eq 0 ]; then echo "$i" | awk -F"|" '{print $3}' >> /tmp/file1; else echo "$i" >> /tmp/file1; fi; done;
#!/bin/sh
for i in `echo test.txt | xargs grep '^>'`; do
local_var=$(echo $i | awk -F '|' '{print $3}')
sed -i "/${local_var}/s|>.*|>${local_var}|" test.txt
done
#!/bin/sh
for i in `echo test.txt | xargs grep '^>'`; do
local_var=$(echo $i | awk -F '|' '{print $3}')
sed -i "/${local_var}/s|>.*|>${local_var}|" test.txt
done
for i in `cat /tmp/file`
do
echo "$i" | grep "^>"
if [ $? -eq 0 ]
then
echo "$i" | awk -F"|" '{print $3}' >> /tmp/file1
else
echo "$i" >> /tmp/file1
fi
done;
If doing it in shell, here's one approach
Code:
while IFS="|" read a b c
do
case "$a" in
">"* ) echo ">$c";;
*) echo $a"|"$b"|"$c
esac
done < "file"
instead of calling grep and awk for each line, you can use the shell's internal functions. much faster that way
Quote:
OR a one liner
Code:
for i in `cat /tmp/file`; do echo "$i" | grep "^>"; if [ $? -eq 0 ]; then echo "$i" | awk -F"|" '{print $3}' >> /tmp/file1; else echo "$i" >> /tmp/file1; fi; done;
Hope this helps.
never cram your code into such a one liner like that.
Could anyone help me set up a script to make this happen? It seems like sed and awk could both do this easily but I can't figure out the syntax to tell either to remove all text except the > before the |LGIG|.
Thanks!
Kevin
Code:
cat data.txt | while read line
do
[[ $line =~ ">" ]] && echo ">${line##*|}" || echo "$line"
done
of course. when you pipe this echo to xargs, xargs just take this "test.txt" and pass to grep as an argument. that's all there is, an extra redundant step.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.