LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep question (https://www.linuxquestions.org/questions/linux-newbie-8/grep-question-662012/)

tekmann33 08-11-2008 10:28 AM

grep question
 
I have a large text file that has the following information in it. This is just a snippet of it:

Code:

3|9|JF HEN||
4|9|J COATSWORTH||
7|3|JF CHO #88|#88|
11|3|JF CHO #41|#41|
23|4|JF CHO #11|#11|
32|5|J 001.96 SCH||
44|2|J 170 P KAL||
46|9|1978 CALDECOTT AWARD||
46|10|J 222 SPI||
64|5|J 551.6 PRI||
79|6|EJ SCH||
86|6|342 BIL||
88|5|J 342.73 MAE||
102|4|EJ WIL||
109|3|J 617.7 ALE||
117|6|EJ BRO||
118|5|J 363.7 FIF||
122|4|973.3 LOE||
129|4|J 385.097 FRA||
132|8|J 391 ROW||
137|5|J 641.09 PEN||
143|7|EJ GEO||
150|10|J 394.2649 GIB||
157|3|J 398 PER||
170|8|J 398.2 BRE||
171|11|BB BRE||
171|12|EJ BRE||

I want to do the following things:
1. edit out the double pipe " || " only at the end of the line and redirect that info to a new file.

2. Edit out any text only at the end of the line that is between a double pipe (example |#11| )and redirect that to a new file.

pixellany 08-11-2008 11:22 AM

Quote:

edit out the double pipe " || " only at the end of the line and redirect that info to a new file.
redirect what info to the new file??


What reference do you use for scripting? I strongly recommend "Bash Guide for Beginners"---free at http://tldp.org

To redirect something to a file, simply put this at the end of the command string: "> newfilename"

There are several ways to grab a specific string, eg:
grep -o string filename ##prints every occurrence of "string"

To get only the text inside the last set of "|"s:
sed -n 's/.*|\(.*\)|$/\1/p' filename
(Actually, this syntax yields what is inside the last pair of "|"s at the end of the line.) To match something not at the end of the line, some more tweaks are required.


All times are GMT -5. The time now is 04:56 AM.