Finding a string in a text file and posting part of the line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a string in a text file and posting part of the line
# 1  
Old 03-24-2010
Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2):
searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found.
In this case, it's known that there are no duplicates in the file, so it either finds the string on one line or none of them.


I'm guessing one of grep, sed or awk can be used but I can't figure out the best way.
# 2  
Old 03-24-2010
Code:
sed -n 's/^helloworld//p' file

Code:
 sed -n '/^hello/s/..........//p' file

# 3  
Old 03-24-2010
maybe like this

Code:
# cat 1
thisiscomm11thisisthisis
thisiscomm22thisisthisis
thisiscomm33thisisthisis

# sed 's/^thisiscomm//g' 1
11thisisthisis
22thisisthisis
33thisisthisis


Last edited by Scott; 03-24-2010 at 05:59 PM.. Reason: Code tags, please...
# 4  
Old 03-24-2010
Try this also using awk:
Code:
awk '{if (substr($0,1,10) == "helloworld") {print substr($0,11)} else {print substr($0,1)}}' file


Last edited by Scott; 03-24-2010 at 05:59 PM.. Reason: Code tags, please...
# 5  
Old 03-24-2010
Quote:
Originally Posted by busdude
searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found.
Code:
awk -v v=string 'v==substr($0,1,10){print substr($0,11,10)}' infile


Last edited by danmero; 03-25-2010 at 08:17 AM.. Reason: Correct output.
# 6  
Old 03-24-2010
Correct one is :
Code:
awk '{if (substr($0,1,10) == "helloworld") {print substr($0,11,20)} else {print substr($0,1)}}' file


Last edited by Scott; 03-24-2010 at 06:00 PM.. Reason: Code tags
# 7  
Old 03-24-2010
Jairaj
  1. Always read the OP requirement.
  2. Please use [code] tags when you post code.
  3. Don't double-post !
  4. Your solution is wrong , check the OP.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding pattern in a text file and returning a part of the word

Dear All, assume that we have a text file or a folder of files, I want to find this pattern followers*.csv in the text file , and get * as the output. There are different matches and * means every character. Thank you in advance. Best, David (1 Reply)
Discussion started by: davidfreed
1 Replies

2. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

6. Shell Programming and Scripting

Reformatting single column text file starting new line when finding particular string

Hi, I have a single colum file and I need to reformat the file so that it creates a new line every time it come to an IP address and the following lines are corresponding rows until it comes to the next IP address. I want to turn this 172.xx.xx.xx gwpusprdrp02_pv seinwnprd03... (7 Replies)
Discussion started by: kieranfoley
7 Replies

7. Shell Programming and Scripting

How to insert string at particular 4th line in text file

I just want to add a particular string in text file using shell script text file format 1 columns-10 2 text=89 3 no<> 4 5 test-9876 6 size=9 string need to insert in 4th line <switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'> After inserting the... (8 Replies)
Discussion started by: puneet.goel
8 Replies

8. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

9. Shell Programming and Scripting

Finding part of a string

Hi I am very new to KSH programming and need some help with finding a string in an error log currently i am doing cat FTP_LOG.lis | grep Warning which gives me Warning: Authentication failed. Remaining authentication methods: 'publickey,pas I want to only pick up the test between the... (4 Replies)
Discussion started by: DAFNIX
4 Replies

10. Shell Programming and Scripting

Finding a certain string on each line in a file

Hi, I need a script to get every line from a file where there are less then 17 ; on a line. Thank's (5 Replies)
Discussion started by: VODAFUN
5 Replies
Login or Register to Ask a Question