find the string in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find the string in a line
# 1  
Old 09-28-2009
find the string in a line

Hello

I have a abc.txt file which semicolon delimited. I need to read the first line of the abc.txt file where i need to extract specific string which at specific location.

i.e first line of the abc.txt file is as below
abc;123;xyz;345;678

my requirement is i need to get the 678. I was trying with awk but not successful

thanks for the help
# 2  
Old 09-28-2009
Something tells me that it's not the whole of your input file ( and not the whole of your goal)...
getting the end of the only line you give as a sample is easy, but from an entire file, it might be more difficult...
if you're just looking for a line that ends with '678', `grep` could do the the job, couldn't it?
for any other pattern, please define the "delimiters" of the 'expression' you're looking for in the file...

please be more precise.
# 3  
Old 09-29-2009
hi
Thanks for the reply.
It might not be the end of the line.Let say if i want to extrct the string out of 3 rd and 4th delimter; then which command should i use. if i want 345 then which command should i use.

Thnaks

---------- Post updated at 11:26 PM ---------- Previous update was at 11:17 PM ----------

Infact my aim is to read the first line of the file and out of the first line i need to read the string which is in between semicolons.
# 4  
Old 09-29-2009
go on, refine your thinking...

if you have a fixed delimiter (kind of ':' or ';' or ',' ...), you should be able to use `cut`; why shouldn't you
# 5  
Old 09-29-2009
$ cat abc.txt
abc;123;xyz;345;678
$ cat abc.txt | perl -e 'while (<>){ chomp; @fields = split(";"); last;} print "$_\n" foreach (@fields);'
abc
123
xyz
345
678

Cheers
# 6  
Old 09-29-2009
one way..
Code:
-bash-3.2$ cat file
abc;123;xyz;345;678
dwad;231;3213;434;343
-bash-3.2$ awk -F";" '{print $1,$2,$3,$4}' file
abc 123 xyz 345
dwad 231 3213 434
-bash-3.2$ awk -F";" '{print $1,$2,$3}' file
abc 123 xyz
dwad 231 3213
-bash-3.2$

# 7  
Old 09-29-2009
depend on your different request:

Code:
awk -F";" '{print $5}' abc.txt

awk -F";" '{print $NF}' abc.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find string and delete before just in line?

Hello, When my lines contain question mark, I use below command to delete the portion of the matching line coming after question mark: sed 's/?.*//' SampleFile SampleFile: helloworldfirstline?mdksmyymsss hellosecondlineworld?mdksmkkmsss thirdhelloworld?mdksmccmsss Output:... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Find a string and its position in a line from another string

Hello guys, would you please help me with this? this is the line inside a file: first line Something Today YYDDPPSVXIPYYY0XXXOFFS00000000000? I'd like to find the position of string XXX from string PYYY In the example above XXX starts from 6th position from PYYY desired... (4 Replies)
Discussion started by: netrom
4 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

Find a string occurrence if twice in a line

Hello All, I want to check if a delimiter is existing twice in a line of a text file. Suppose flat file is like this 234 | 123 123 | 345 456 | 563 | 234 | 548 So the the 3rd line has two delimiters, How can we find the lines in such a file having more then one delimiters I tried... (5 Replies)
Discussion started by: nnani
5 Replies

5. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

6. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

7. UNIX for Dummies Questions & Answers

Find string multiple times, same line

Hi everybody, Fairly simple question here: I need an awk, sed, or grep command that will find the same string multiple times on one line needs to return all lines which contain .02 twice. I do know the exact number of characters in between the two occurrences of .02 if that helps, all... (7 Replies)
Discussion started by: jgrosecl
7 Replies

8. Shell Programming and Scripting

Find line containing string in a file.

Hello. I have a large file that contains a lot of gibberish and also a lot of http addresses. How can i read the file, take out the http addresses, and write each one of them on one line each into another file? It looks something like this. ... (7 Replies)
Discussion started by: cbreiny
7 Replies

9. Programming

Find a line containing a string.

Hello. I have a large file that contains a lot of gibberish and also a lot of http addresses. How can i read the file, take out the http addresses, and write each one of them on one line each into another file? It looks something like this. ... (1 Reply)
Discussion started by: cbreiny
1 Replies

10. Shell Programming and Scripting

grep string and find line before

hi, i have to grep for string in file but i want to find the group of this line so i must get lines before and select the group. the file look like : ####name_groupe1 alphanumeric line alphanumeric line .. ####name_groupe2 alphanumeric line alphanumeric line .. ####name_groupe3... (4 Replies)
Discussion started by: kamel.seg
4 Replies
Login or Register to Ask a Question