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
15 19648
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?
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
prn 254
Recognized Expert Contributor
How about : - #! /bin/sh
-
echo -n "Input your name: "
-
read NAME
-
echo "Name to find is: $NAME"
-
grep -e "\d* $NAME " my_file.txt
-
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
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
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
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. -
awk '$2 == 7 {print }' "file" #or awk '$2 == 7' "file"
-
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: -
awk '$2 == 7 && $3 == 8 {print }' "file" # or awk '$2 == 7 && $3 == 8' "file"
-
also in awk: -
awk '$2 == 7 && $3 == 8 {print }' "file" # or awk '$2 == 7 && $3 == 8' "file"
-
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.
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
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.
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 -
echo "Enter input"
-
read input
-
awk -v input=$input '$2 == 7 && $3 == input {print }' "file"
-
then you can do this for example -
echo "Enter input"
-
read input
-
awk -v input=$input '$2 == 7 && $3 == input {print }' "file"
-
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
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?
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: -
# read from file, replace 1st ':' with space, then print column #4 only
-
sed <inputfile s/:/\ / | awk '{print $4;}'
-
Regards,
Bernd
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
by: Dixie |
last post by:
Is there a way to shell to Microsoft Word from Access and load a specific
template - using VBA?
dixie
|
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...
| |
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:...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |