Need some help deleting words from a line which are not my "Keyword"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need some help deleting words from a line which are not my "Keyword"
# 8  
Old 07-18-2010
Code:
sed '/abc/s/\(.*[ \t]*\)\(abc.[^ \t]*\)\(.*\)/\2/' file

# 9  
Old 07-19-2010
Thanks for the reply Radoulov (and others). As I could not access my documents yesterday I have discovered a slight problem, and although the solution partially works it doesn't do as required, which is partly my fault. The ID number is actually of the format:

abcd-p-ssa-a322f-s-4312

with the hyphens, numbers, and letters in that order. Currently the solution will only print upto the abc and not everything else. However, to add to my misery, there are also other types of ID of the format:

abcf-p-ssa-a322f-s-4312 (the abc here still remains)
def-abcf-p-ssa-a322f-s-4312 (addition of def- at the start of the two above)
def-abcd-p-ssa-a322f-s-4312

The first few sets of lettters always remain the same (i.e. abc-p-ssa , abcf-p-ssa , def-abcf-p-ssa , def-abcd-p-ssa) . Sorry for the inconvienence caused (I probably spelt that wrong!). Thank you for your time.

---------- Post updated at 10:45 AM ---------- Previous update was at 09:29 AM ----------

Quote:
Originally Posted by guruprasadpr
Hi

Code:
sed  '/abc/s/.*\(abc[0-9][0-9]*\).*/\1/' file

Guru.
Hey, I tried this out but it cuts out the last line of my file for some reason. Also if you could, my requirements have now changed slightly Smilie they are written above. Thanks

Last edited by linuxkid; 07-19-2010 at 06:41 AM.. Reason: Additional detail
# 10  
Old 07-19-2010
Code:
perl -nle'
   /abc/ and print join " ", /abc[\w-]+/g
     or print
    ' infile

# 11  
Old 07-19-2010
Quote:
Originally Posted by radoulov
Code:
perl -nle'
   /abc/ and print join " ", /abc[\w-]+/g
     or print
    ' infile

Thats world class mate! But if I also wanted to add the option to search for lines with the def instead of abc in addition to the abc, how would I do that? Is there some sort of OR operator?
# 12  
Old 07-19-2010
Code:
perl -nle'
  /(?:abc|def)/ and print join " ", /(?:abc|def)[\w-]+/g      
    or print     
    ' infile

This User Gave Thanks to radoulov For This Post:
# 13  
Old 07-19-2010
Quote:
Originally Posted by radoulov
Code:
perl -nle'
  /(?:abc|def)/ and print join " ", /(?:abc|def)[\w-]+/g      
    or print     
    ' infile

Brilliant! Thanks mate, really appreciate the help!
# 14  
Old 08-19-2010
Quote:
Originally Posted by radoulov
Code:
perl -nle'
  /(?:abc|def)/ and print join " ", /(?:abc|def)[\w-]+/g      
    or print     
    ' infile

sorry to bring this up again, but since this answer theres been a few changes in my input file,mainly the addition of a new set of numbers

Code:
abcf-p-ssa-a322f-s-4312 
def-abcf-p-ssa-a322f-s-4312 
def-abcd-p-ssa-a322f-s-4312
#below is the new addition
ghi/123/xxx/sss/xa2
ghi/3d/cksdi/kff/23 def-abcd-p-ssa-a322f-s-4312

the ghi always remains at the start, so the problem is that the code you gave doesn't work because of the brackets in this new set, as I tried:
Code:
perl -nle'
  /(?:abc|def)/ and print join " ", /(?:abc|def)[\w-]+/g      
    or print     
    ' infile

but that just ignored anything with brackets! I also removed the '+' sign after [\w-] but that gave me the ghi, but not the rest of the number! Also as in teh last line, some of these may be on the same line, but your code already seems to work for that anyway (excpet when its got '/' in the number, so I need to keep that functionality)

Many thanks

Last edited by radoulov; 08-23-2010 at 06:28 AM.. Reason: Fixed code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

4. Shell Programming and Scripting

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line... (4 Replies)
Discussion started by: sudeep.id
4 Replies

5. UNIX for Dummies Questions & Answers

Deleting "user input line number" from a file using sed

Hi I want to delete a line from a txt file for which the line number is user input. Say when user selects 19, the 19th line would be deleted from the file. Can anyone please provide me with a sed one liner for the same... I tried sed -i. The interaction would be like this Enter the line to... (1 Reply)
Discussion started by: sudeep.id
1 Replies

6. Shell Programming and Scripting

Error for "continue" keyword in Linux script.

Hi All I have a function in a linux script like this clean_up() { db2 -x "UPDATE ${DB_SCHEMA_NAME}.ETL_DAILY SET ETL_STATUS = 'SUCCESSFUL' WHERE PROCESS_DATE = '${INT_RUN_DATE}' AND BATCH_NO = ${CM_BATCH} AND APP_ID = ${APP_ID} AND APP_VERSION = '${APP_VERSION}'" > ${TMPOUT} ... (3 Replies)
Discussion started by: vee_789
3 Replies

7. AIX

"Frames" and "Words" in fcstat output

What are "Frames" and "Words" in the fcstat output? vio1:/home/padmin:# fcstat fcs0 <snip> Transmit Statistics Receive Statistics ------------------- ------------------ Frames: 122844229 363445456 Words: 50940091904 171210861568 <snip> The... (1 Reply)
Discussion started by: kah00na
1 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question