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
# 8  
Old 03-24-2010
Hi, danmero:

I agree. Jairaj's proposed solution is definitely incorrect. However, without some clarification from the original poster, yours could be as well.

If the string being searched must occur in the first ten characters, but can itself be less than 10 characters, it could then occur at different locations within the first 10 characters. If this scenario is a possibility, then
Code:
v==substr($0,1,10)

would be insufficient.

If this is the case, then the following would be the correct approach:
Code:
awk -v v=string 'i=index($0,v){if (i+length(v)-1<=10) print substr($0,11,10)}'

Again, I think the original poster's problem statement is a bit too vague, leaving some wiggle room for this possibility.

Regards,
Alister

Last edited by alister; 03-25-2010 at 02:38 AM..
# 9  
Old 03-25-2010
Quote:
Originally Posted by busdude
searching the first 10 characters of every line in a text file for a specific string
Quote:
Originally Posted by alister
If the string being searched must occur in the first ten characters, but can itself be less than 10 characters
This is not about right vs. wrong OR what if suppositions.
The OP ask to "searching the first 10 characters" and that's a fact and we should start from here. Everything else are only suppositions.
I can provide answers/solutions(short) base on facts, otherwise the list will be too long Smilie

Regards,
丹 ™
# 10  
Old 03-25-2010
Quote:
Originally Posted by danmero
Code:
awk -v v=string 'v==substr($0,1,10){print substr($0,11,20)}' infile

Your solution is incorrect, regardless (as was mine). The problem requested printing characters 11-20 if a match is found; that second substr call returns 11-30 (the third argument is length, not an index). The 20 should be a 10.

Regards,
Alister
# 11  
Old 03-25-2010
Quote:
Originally Posted by alister
Your solution is incorrect, .... The 20 should be a 10.
That's correct, I fix my original post Smilie
# 12  
Old 03-25-2010
Quote:
Originally Posted by busdude
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.
...
Well, Perl scripts are known to be succinct... Smilie
For the dummy file below, assuming the search string is the 10 character "helloworld" -

Code:
$
$ cat f2
helloworld>abcdefghijklmnopqrstuvwxyz
holamundo>abcdefghijklmnopqrstuvwxyz
helloworld>ABCDEFGHIJKLMNOPQRSTUVWXYZ
helloworld>0123456789
bonjourmonde>abcdefghijklmnopqrstuvwxyz
hallowelt>ABCDEFGHIJKLMNOPQRSTUVWXYZ
$
$
$ perl -lne 'print $1 if /^helloworld(.{10})/' f2
>abcdefghi
>ABCDEFGHI
>012345678
$
$

tyler_durden
# 13  
Old 03-25-2010
what is wrong Mr danmero?
# 14  
Old 03-25-2010
Quote:
Originally Posted by Jairaj
Code:
awk '{if (substr($0,1,10) == "helloworld") {print substr($0,11,20)} else {print substr($0,1)}}' file

Code:
awk '{if (substr($0,1,10) == "helloworld") {print substr($0,11,10)}}'

You don't want to print the record if no match(your else). My mistake , I take the 20 from you Smilie see alister comments above.

To simplify we can write
Code:
awk 'substr($0,1,10) == "helloworld" {print substr($0,11,10)}'

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