473,503 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get a specific word in a file?

60 New Member
hi,

how can i get the particular line/word in the file.

Ex:
$ cat my_file.txt
1
2
3
4
5
6
7

I want to get the "7" in the file my_file.txt. Tnx.


-coax
Jun 21 '07 #1
15 19648
mac11
256 Contributor
I suppose there is a million ways to do this. The command line:

cat file.txt | grep "7"

would "get" the 7, but what do you want to do with the 7 after you "get" it?
Jun 22 '07 #2
coaxfiber
60 New Member
What I want is to have exactly that specific line?
Intentionally I used AWK but I want to have my input as my search text.
this is what exactly what i mean,


$ Input your name: 7

And then I want to get that 7 from the file my_file.txt which contains the following inside the file:

$cat my_file.txt

102 7 SanFracisco
102 3 Lasvegas7
105 5 Nevada7
106 7 Japan


If I will going to use "cat file.txt | grep "7" then surely I'll get the following result


102 7 SanFracisco
102 3 Lasvegas7
106 7 Japan


which means that all lines contain '7' will be the result.

What I want to be the result:

102 7 SanFracisco
106 7 Japan

only the line which the second word is 7.

-thanks
Jun 22 '07 #3
prn
254 Recognized Expert Contributor
How about:

Expand|Select|Wrap|Line Numbers
  1. #! /bin/sh
  2. echo -n "Input your name: "
  3. read NAME
  4. echo "Name to find is: $NAME"
  5. grep -e "\d* $NAME " my_file.txt
  6.  
This appears to do what you are looking for. If this file is named "find.sh" and your data is in "my_file.txt" then

[prn@deimos ~]$ ./find.sh
Input your name: 7
Name to find is: 7
102 7 SanFracisco
106 7 Japan

HTH,
Paul
Jun 22 '07 #4
ghostdog74
511 Recognized Expert Contributor
I suppose there is a million ways to do this. The command line:

cat file.txt | grep "7"

would "get" the 7, but what do you want to do with the 7 after you "get" it?
this is Useless use of cat UUOC. grep takes in a file as input
Expand|Select|Wrap|Line Numbers
  1. grep "7" inputfile
  2.  
Jun 22 '07 #5
coaxfiber
60 New Member
Yeah, "grep -e "\d* $NAME " my_file.txt"


This will really a big help for my script.
I tried to 'man grep', i dont usually look for the manual to seek help because sometimes it's more complicated than to ask someone like you.

Can you tell me what is the purpose of " \d* " in the grep statement you have given to me? Then one more thing again,

How about when if it is the 3 word in the line I want to grep.
Now I need the 8. I think after this I could use grep better.

102 7 8 SanFracisco
102 3 8 Lasvegas7
105 5 5 Nevada7
106 7 2 Japan

> 102 7 8 SanFracisco
> 102 3 8 Lasvegas7

I wish I give sense to this. Thank You Very Much.

-coax
Jun 23 '07 #6
ghostdog74
511 Recognized Expert Contributor
What I want is to have exactly that specific line?
Intentionally I used AWK but I want to have my input as my search text.
this is what exactly what i mean,


$ Input your name: 7

And then I want to get that 7 from the file my_file.txt which contains the following inside the file:

$cat my_file.txt

102 7 SanFracisco
102 3 Lasvegas7
105 5 Nevada7
106 7 Japan


If I will going to use "cat file.txt | grep "7" then surely I'll get the following result


102 7 SanFracisco
102 3 Lasvegas7
106 7 Japan


which means that all lines contain '7' will be the result.

What I want to be the result:

102 7 SanFracisco
106 7 Japan

only the line which the second word is 7.

-thanks
it can be done in awk.
Expand|Select|Wrap|Line Numbers
  1. awk '$2 == 7 {print }' "file"  #or awk '$2 == 7' "file"
  2.  
Jun 24 '07 #7
ghostdog74
511 Recognized Expert Contributor
Yeah, "grep -e "\d* $NAME " my_file.txt"


This will really a big help for my script.
I tried to 'man grep', i dont usually look for the manual to seek help because sometimes it's more complicated than to ask someone like you.

Can you tell me what is the purpose of " \d* " in the grep statement you have given to me? Then one more thing again,

How about when if it is the 3 word in the line I want to grep.
Now I need the 8. I think after this I could use grep better.

102 7 8 SanFracisco
102 3 8 Lasvegas7
105 5 5 Nevada7
106 7 2 Japan

> 102 7 8 SanFracisco
> 102 3 8 Lasvegas7

I wish I give sense to this. Thank You Very Much.

-coax
also in awk:
Expand|Select|Wrap|Line Numbers
  1. awk '$2 == 7 && $3 == 8 {print }' "file" # or awk '$2 == 7 && $3 == 8' "file"
  2.  
Jun 24 '07 #8
coaxfiber
60 New Member
also in awk:
Expand|Select|Wrap|Line Numbers
  1. awk '$2 == 7 && $3 == 8 {print }' "file" # or awk '$2 == 7 && $3 == 8' "file"
  2.  
Hi.. I think I almost found exactly what will really answer my question.. What if the third word is not always '8'. How about it will depend on my input?
$Input ID: 6
then it will search for all lines that contain 6 in the 3rd word. Thanks.
Jun 24 '07 #9
prn
254 Recognized Expert Contributor
Can you tell me what is the purpose of " \d* " in the grep statement you have given to me? Then one more thing again,

How about when if it is the 3 word in the line I want to grep.
Now I need the 8. I think after this I could use grep better.
"\d* " means an arbitrary number of digits (\d) followed by a space.

As for the question of "8" as the third word, I might suggest you rethink the whole premise of the question.

The more different things you want your script to do, the more you need to write with that in mind from the beginning. You have now asked for 3 pretty different criteria (just search for "7", "7" as the second "word", and now "8" as the third "word"). If you want something so general that it can do any one or all of these, then you need to decide from the beginning what information you need, how you want to interpret that information and then what you want to do with it. I don't think it's very profitable for any of us to answer a question and then be asked again: "OK, but what if ...?"

Take a short break and think about what you are really looking for. Ghostdog74 is probably on the right track if you want to manipulate "words" on lines. awk is a good choice for that. If you're going off in some other direction, then awk may (or may not) turn out to be best for that. So far, we're all just proposing answers based on the couple of examples you give us at a time. Think about what your real data might look like and what you really want to extract from it.

Best Regards,
Paul
Jun 25 '07 #10
coaxfiber
60 New Member
Yes, I agree to you.I want to give some apology if I had to overuse this forum for my own benefit. Perhaps I need to concentrate on my work. I hope I could also help some people who also needs some support.. Thanks.
Jun 25 '07 #11
ghostdog74
511 Recognized Expert Contributor
Hi.. I think I almost found exactly what will really answer my question.. What if the third word is not always '8'. How about it will depend on my input?
$Input ID: 6
then it will search for all lines that contain 6 in the 3rd word. Thanks.
then you can do this for example
Expand|Select|Wrap|Line Numbers
  1. echo "Enter input"
  2. read input
  3. awk -v input=$input '$2 == 7 && $3 == input {print }' "file" 
  4.  
Jun 25 '07 #12
coaxfiber
60 New Member
then you can do this for example
Expand|Select|Wrap|Line Numbers
  1. echo "Enter input"
  2. read input
  3. awk -v input=$input '$2 == 7 && $3 == input {print }' "file" 
  4.  
Thank You Sir! This is exactly what I need. I had tried to used the ' -v ' option before but some syntax came to error.

BR,
-coax
Jun 26 '07 #13
marimuthup
1 New Member
with awk or grep how would I get a specific word in a file, rather than a lin.

For example:

File contains lines such as

This is MyTime:00:0450 Secs

I wanna to extract the String where it is between 'MyTime:' and Secs, where I need only the String '00:0450' printed.

Definelty not the entire row.

Could some one help me?
Aug 16 '07 #14
docdiesel
297 Recognized Expert Contributor
with awk or grep how would I get a specific word in a file, rather than a lin. For example:

File contains lines such as
This is MyTime:00:0450 Secs
....
Hi,

perhaps you should've opend a new thread for your question. Anyway, what may fit your needs is a line like this:

Expand|Select|Wrap|Line Numbers
  1.  # read from file, replace 1st ':' with space, then print column #4 only
  2.  sed <inputfile  s/:/\ /  |  awk '{print $4;}'
  3.  
Regards,

Bernd
Aug 16 '07 #15
coaxfiber
60 New Member
with awk or grep how would I get a specific word in a file, rather than a lin.

For example:

File contains lines such as

This is MyTime:00:0450 Secs

I wanna to extract the String where it is between 'MyTime:' and Secs, where I need only the String '00:0450' printed.

Definelty not the entire row.

Could some one help me?

Hi,


Try this one assuming line 'MyTime:00:0450 Secs' is fix and only the numbers are varying,


cut -c 8-14 file.txt

FYI.

-coax
Aug 16 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1619
by: MM | last post by:
Hello, Can anyone help me with this: I would like to use a help functionality in my program, in such way that a specific page (or topic) in the help file (a standard Windows .hlp file) is...
1
1526
by: DP | last post by:
hi, i know how to open ms word in access, but is there a way to open a specific file?? i've a created a mail merge letter, and i want to be able to click on a button in access, and it opens...
5
2746
by: akelly_image | last post by:
Okay, if anyone could toss me some idea's here, please bare with my noobish questions, I just picked up VB2005 Pro about a week ago. ( no prior VB at all ) Here's my issue.. I'm pulling...
12
10635
by: Dixie | last post by:
Is there a way to shell to Microsoft Word from Access and load a specific template - using VBA? dixie
6
2335
by: vincent90152900 | last post by:
always get an error message. "Object reference not set to an instance of an object" Can anyone how to solve this? Here is my code. private void CreateNewWordFile(string sourceFileName,string...
2
19721
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
2061
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
2
2100
by: dpw.asdf | last post by:
I have been searching all over for a solution to this. I am new to Python, so I'm a little lost. Any pointers would be a great help. I have a couple hundred emails that contain data I would like to...
3
4327
by: evenlater | last post by:
I have an Access application on a terminal server. Sometimes my users need to export reports to pdf, rtf or xls files and save them to their own client device hard drives. They can do that right...
3
3161
by: Sevla | last post by:
hello, im having a problem to change a particular word in a line, canīt find where is my mistake also i would like to know how to make this program check automatically if a particular word...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.