find a word in a file, plus the next 6 characters?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find a word in a file, plus the next 6 characters?
# 1  
Old 12-29-2010
find a multiple words on a line, plus the next 6 characters?

I plan to use sed in a script to replace a string. My problem is the last 6 characters of the word to be replaced can be different each time, plus it's not always in the same spot on the line so I can't use cut or nawk to get the field. So I am looking for a way to find a certain word in a file, plus the next 6 characters.

My file looks like this:
Code:
abc=word1,bcd=word2,bcd=word3 abc=word1_a123456,bcd=word,bcd=word:abc=word2,bcd=word,bcd=word:abc=word3,bcd=word,bcd=word
abc=word1,bcd=word2,bcd=word3 abc=word2,bcd=word,bcd=word:abc=word1_a987654,bcd=word,bcd=word,bcd=word:abc=word3,bcd=word,bcd=word:abc=word1_a345678,bcd=word,bcd=word:

What I am looking for in the file is word1_a######. The word1_a portion will always be the same, but the numbers can change. It can also happen multiple times in one line. I need to find out what the numbers are that go with word1_a so I can pass them as a variable later to change it in sed.

Any help is greatly appreciated!

Last edited by mikayla73; 12-30-2010 at 10:43 AM.. Reason: update title with more detail
# 2  
Old 12-29-2010
Like this?

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

# 3  
Old 12-30-2010
My file looks like this now

Code:
word1_a123456
word1_a987654

It gives me the number, but also changes the entire line of the file to just have word1_a123456 as that line. I don't want to change the whole line, just find the word1_a#'s so I can use it as a variable later. I also need all instances of word1_a#'s on the same line. It only shows me the last instance on the line.

I'm not actually looking to replace the information I find yet, just find it and figure out what the 6 numbers are after it.

Ultimately my script will replace the information that is between the :'s where the word1_a is found, but I have to figure out what the 6#'s are so I can change the correct portion of the line.

Orignal file:
Code:
abc=word1,bcd=word2,bcd=word3 abc=word1_a123456,bcd=word,bcd=word:abc=word2,bcd=word,bcd=word:abc=word3,bcd=word,bcd=word
abc=word1,bcd=word2,bcd=word3 abc=word2,bcd=word,bcd=word:abc=word1_a987654,bcd=word,bcd=word,bcd=word:abc=word3,bcd=word,bcd=word

Ending result:
Code:
abc=word1,bcd=word2,bcd=word3 newinfo:abc=word2,bcd=word,bcd=word:abc=word3,bcd=word,bcd=word
abc=word1,bcd=word2,bcd=word3 abc=word2,bcd=word,bcd=word:newinfo:abc=word3,bcd=word,bcd=word

I plan on using sed to put the newinfo in the file, once I figure out what the numbers are after word1_a.

Thanks again!

---------- Post updated 12-30-10 at 07:50 AM ---------- Previous update was 12-29-10 at 10:10 AM ----------

I have this sort of working. I am using the following code (thanks to verdepollo) with the addition of the the -n -e to get only the string I am looking for. However, if there are multiple instances of the string on one line, I am only getting the last one. I need to get all of them, where ever they are in the file.

Code:
sed -n -e "s/.*\(word1_a[0-9][0-9][0-9][0-9][0-9][0-9]\).*/\1/p" inputfile > outputfile

Is there a way to do that?
# 4  
Old 12-30-2010
Using GNU grep:

Code:
grep -o 'word1_a[0-9][0-9][0-9][0-9][0-9][0-9]' infile

This User Gave Thanks to verdepollo For This Post:
# 5  
Old 12-30-2010
THANK YOU!!

I knew that grep -o would find the word instead of the entire line, I didn't know I could use the [0-9] to get the additional characters.

Thanks again!
# 6  
Old 01-03-2011
Quote:
Originally Posted by verdepollo
Using GNU grep:

Code:
grep -o 'word1_a[0-9][0-9][0-9][0-9][0-9][0-9]' infile

Not positive, but I think you could do
Code:
grep -oE 'word1_a[0-9]{6)' infile

which is a bit simpler.

Also, if you have lines with word1_a123 it will also find that I believe, as both of the grep options will find shorter matches too. You may want to specify it has to be 6 numbers in your code if there is a chance that other shorter or longer matches exist.

Last edited by glev2005; 01-03-2011 at 12:46 PM..
# 7  
Old 01-03-2011
Thanks that does work too, with one minor change. The ")" has to be a "}", which I am guessing was just a typo. Smilie

Code:
grep -oE 'word1_a[0-9]{6}' infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count characters in a csv file and add an word.

Hello, I want to add a sentence to "post column" those who are only less than 30 characters.Thank you very much for your help. "category","title","post" "Z","Zoo","test 54325 test 45363mc." "Z","Zen","rs2w3rsj 2d342dg 2d3s4f23 d23423s23h 2s34s2423g ds232d34 2342." "Z","Zet","test4444... (3 Replies)
Discussion started by: hoo
3 Replies

2. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

3. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

4. UNIX for Dummies Questions & Answers

Find word in a large file

Hi all I am working on disallowing users to use easy passwords in pam.d setting on RHEL 5.7 and SuSe 11, and I was hoping to add more words into the current cracklib dict, so I use "echo" command to append new words into the file I dont want to add the same words into the dict, I think I... (2 Replies)
Discussion started by: hedkandi
2 Replies

5. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

6. Shell Programming and Scripting

How to find a particular word from a file

Hello Experts, I have to count the word like "RESULT_CODE: : -6" from the multiple files names like req.result_2_vqx-71144750.log for a particular date. Lets suppose the date is 10 OCT 2011. How I can do it with a single command in Solaris environment. Reagrds Oracle User (8 Replies)
Discussion started by: Oracle_User
8 Replies

7. Shell Programming and Scripting

Find word in file then get following characters

Hello, I have several xml files from which I want to find and return a particular string I want to locate the InId="00000008". Now that is inlcuded within a tag and ofcourse the number is different every time this is what I came up with given that after greping the line that contains the... (6 Replies)
Discussion started by: TasosARISFC
6 Replies

8. Shell Programming and Scripting

find replace a pattern and following characters in a word

Suppose that I have a string "one:#red two:#yellow three:#gr'een four:#blu^e" and I want to replace the pattern :# and the following characters in the word with nothing. The output string should look "one two three four" How can I do this with sed. Some points to consider (a) the last word in... (1 Reply)
Discussion started by: superuser84
1 Replies

9. Shell Programming and Scripting

Find Exact word in file

Hi ALL, I want to search one string “20 “ i.e 20 with space. But my file where I am searching this “20 “ contain some data like 120 before image file truncated 220 Reports section succeeded 20 Transaction database .prd stopped 220 Reports section completed. When I search for the... (5 Replies)
Discussion started by: Jeevan Salunke
5 Replies

10. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies
Login or Register to Ask a Question