Remove keyword found in title


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove keyword found in title
# 1  
Old 07-31-2011
Remove keyword found in title

Hello All,
I have a bunch of files that have the following format, where the title is INPUT.txt and contains the following text:
Code:
INPUT-FILLER1 204
INPUT-FILLER2
FILLER6-INPUT 5
FILLER-INPUT

I want to go through the directory and remove the keyword INPUT. For example, my output would be
Code:
FILLER1 204
FILLER2
FILLER6
FILLER

I have multiple files where the keyword is the title of the .txt file, so I figured bash would be the way to go. I was thinking I would need two sed statements. One to remove the INPUT and the other to remove "-"

Any help would be appreciated.

Last edited by Franklin52; 08-01-2011 at 11:19 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-31-2011
If replacing all strings of the form "INPUT-" or "-INPUT" regardless of where they appear in the file will work for you, then this will do the trick:


Code:
sed 's/INPUT-//; s/-INPUT//' input-file >output-file

If the INPUT- strings appear only at the beginning of the line, then make the first substitute command this: s/^INPUT-//.


Test this carefully -- Not being sure what else your file contains, I'm not 100% sure that this won't replace something meaningful.

Last edited by agama; 07-31-2011 at 11:43 PM.. Reason: corrected typo
# 3  
Old 08-01-2011
thanks. I had to adjust your code slightly (< input file).

I modified it quite a bit to fit into a bash script, but i'm having issues. Any ideas?

PHP Code:
#!/bin/bash
for i in directory/*.txt
do
    var=$(echo $i.txt |cut -d . -f 1)
    sed "s/$var-//; s/-$var//" < "$var".txt  > "$var".tmp
done 
# 4  
Old 08-01-2011
Try
Code:
for i in directory/*.txt
do
    var=$(echo $i |cut -d . -f 1) # no '.txt' after $i
    sed "s/$var-//; s/-$var//" < "$var".txt  > "$var".tmp
done

This User Gave Thanks to yazu For This Post:
# 5  
Old 08-01-2011
yazu/agama, thanks for your continued help.

I tried your suggestion and got a repeated error: "sed: -e expression #1, char 22: unknown option to `s'"

PHP Code:
for i in directory/*.txt
do
    var=$(echo $i |cut -d . -f 1)
    sed "s/$var-//; s/-$var//" < "$var".txt  > "$var".tmp
done 
# 6  
Old 08-01-2011
Debug your script to see what you get:
Code:
for i in directory/*.txt
do
    var=$(echo $i |cut -d . -f 1)
    echo "$var"
    echo sed "s/$var-//; s/-$var//"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

2. Shell Programming and Scripting

Remove not only the duplicate string but also the keyword of the string in Perl

Hi Perl users, I have another problem with text processing in Perl. I have a file below: Linux Unix Linux Windows SUN MACOS SUN SUN HP-AUX I want the result below: Unix Windows SUN MACOS HP-AUX so the duplicate string will be removed and also the keyword of the string on... (2 Replies)
Discussion started by: askari
2 Replies

3. Shell Programming and Scripting

How to remove. (dot) if found in the beginning of file name while doing wget (download)?

Dear All, How to remove. (dot) if found in the beginning of file name while doing wget (download)? I am facing problem while re-sizing the image by using ImageMagick. Two dots in the file name are causing problem. ImageMagick is skipping such image with a dot . in the beginning, like ... (1 Reply)
Discussion started by: Praveen Pandit
1 Replies

4. Shell Programming and Scripting

remove anything after repeated string pattern found

HI, Can anyone help me with a script. i/p calc 1 2 3 4 5 6 7 8 calc 4 5 6 calc 7 8 9 o/p calc 1 2 3 4 5 6 7 8 calc 4 5 6 i.e remove anything after where two times the string calc is found. thanks (3 Replies)
Discussion started by: Indra2011
3 Replies

5. Shell Programming and Scripting

printing a text until a keyword is found

Hi, here's the problem: text="hello1 hello2 world earth mars jupiter planet"how do I print the text until it finds the keyword "mars" so that the desired output is output="hello1 hello2 world earth" I have rtfm of sed and I think the problem is, that if I find the word "mars" it will... (7 Replies)
Discussion started by: icantfindauser
7 Replies

6. Shell Programming and Scripting

How to remove code before second keyword

I have a site where over 1000+ files were injected with code. <?php lots of garbage ?><GOOD HTML or PHP> everything before the < is the injected code i.e., the injected code is: <?php lots of garbage ?> How do I: 1. check that there is in fact an injection (so I don't... (4 Replies)
Discussion started by: vanessafan99
4 Replies

7. Shell Programming and Scripting

read the first 10 lines below a keyword found by grep

Hello Everyone, i need to read specific number of lines ( always serialized ; i.e from 10 to 20 or from 34 to 44 ) in a file , where the first line is found by grep 'ing a keyword. example file.txt ------------------------------------------------------------------ --header this is the... (7 Replies)
Discussion started by: alain.kazan
7 Replies

8. UNIX for Advanced & Expert Users

Remove new line characters if found between 1 to 10 columns

Hi, I have a file with ';' delimeter which has some new line characters. How can I delete the new line characters if they are found between 1 to 10 fields. Thanks (3 Replies)
Discussion started by: rudoraj
3 Replies

9. UNIX for Dummies Questions & Answers

find and remove rows from file where multi occurrences of character found

I have a '~' delimited file of 6 - 7 million rows. Each row should contain 13 columns delimited by 12 ~'s. Where there are 13 tildes, the row needs to be removed. Each row contains alphanumeric data and occasionally a ~ ends up in a descriptive field and therefore acts as a delimiter, resulting in... (1 Reply)
Discussion started by: kpd
1 Replies

10. Solaris

dtwm: Invalid keyword -- /TITLE

Unable to log into solaris 8 CDE as a particular user. The errors are: dtsession: Unable to start message server - exiting. dtwm: Invalid keyword -- /TITLE root can log in successfully. I'm guessing that it's trying to run dtwm with an invalid option. Howcan I correct this? (1 Reply)
Discussion started by: lyonsd
1 Replies
Login or Register to Ask a Question