473,657 Members | 2,801 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 19659
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.tx t" 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

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

Similar topics

5
1628
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 opened directly without me having to type in the topic or search word by hand. In other words, can I access a specific page in a .hlp file?
1
1537
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 that specific document. TIA
5
2754
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 information out of a text file and need to pull specific words out of a string. For example, the text file looks sorta like this:
12
10653
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
2343
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 destinationFileName,string stratBookmarkName,string endBookmarkName) { // Declaring the object variables we will need later object varFileName = fileName; object varFalseValue = false;
2
19742
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: print line f.close()
0
2077
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 446 27.436 34.037 56.145 1.00 0.00 H HETATM 7009 H2 WAT 446 27.049 33.827 54.563 1.00 0.00 H occurring in a 300MB file. In present case each three-lines block is followed by line renumbering (7007,
2
2108
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 incorporate into a database or CSV file. I want to search the email for specific text. The emails basically look like this:
3
4346
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 now the way I have this set up, but it's confusing and slow. When they browse for a place to save the reports, they see all of the drives on the terminal server as well as their own client drives. So they're likely to want to choose "My...
3
3171
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 exists on the line you got to you exchange it or not i didnt find the right condition to do it
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.