- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to grep a string in particular colom?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 02:19 AM
12-11-2007 02:19 AM
I am having two files.
Files1 is having entries like:
Region
"101"
"102"
"103"
and so on. Having unique records.
Other file:
File2:
Region site description
"101","ddd","this is region 101"
"101","ddc","this was region"
"102","dda","this gg region102"
"102","ddn","this oo region"
and so on(Having dulicate region values)".
I want to take record one by one from file 1 and grep only in first fileld of file2 and output the results in other file.
Can some suggest how to do this?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 03:11 AM
12-11-2007 03:11 AM
Re: How to grep a string in particular colom?
you can use "awk" to take only the first column from the file.
Use something like this:
awk -F"," '{ print $1 }' path_to_file2
where "," is the separator between the columns
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 03:12 AM
12-11-2007 03:12 AM
Solutionwhile read file1; do
echo "Looking for: $file1"
awk -F, -v PAT="$file1" '
$1 ~ PAT { print $0 }' File2
done < File1
If you have lots of records, you need to do vector/quantum methods.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 03:27 AM
12-11-2007 03:27 AM
Re: How to grep a string in particular colom?
I am agin simplyfying my question.
My code should be like.
while read line
do
grep "$line" file2 >> output.txt
done < file1
i want to something like that. But i want to grep region value in first field of file 2 (because that string may be present in some other colom also that i do not want to take.)and out complte line corresponding to that in out put file.
We can say files are of normal size then can you suggest how to do.
out file file should have record like :
"101","ddd","this is region 101"
"101","ddc","this was region"
"102","dda","this gg region102"
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 03:40 AM
12-11-2007 03:40 AM
Re: How to grep a string in particular colom?
I suggested how to do it. If you don't want the title you can do:
awk -F, -v PAT="$file1" '
BEGIN { getline }
$1 ~ PAT { print $0 }' File2
You should leave out that debugging "echo".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 04:09 AM
12-11-2007 04:09 AM
Re: How to grep a string in particular colom?
It is working fine.
Can i redirect the out put to output file as i want this?
while read file1; do
echo "Looking for: $file1"
awk -F, -v PAT="$file1" '
$1 ~ PAT { print $0 }' File2 >> output.txt
done < File1
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 04:21 AM
12-11-2007 04:21 AM
Re: How to grep a string in particular colom?
Applogies one more clarification.
In this case i am greping string in first field in File2. If would be 3field in File2 then where i have to make changes.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 05:06 AM
12-11-2007 05:06 AM
Re: How to grep a string in particular colom?
> In this case i am greping string in first field in File2. If would be 3field in File2 then where i have to make changes.
Then, change:
$1 ~ PAT { print $0 }' File2 >> output.txt
To:
$3 ~ PAT { print $0 }' File2 >> output.txt
'awk' numbers fields counting from one (1) with '$0' representing the whole line (record).
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 07:57 AM
12-11-2007 07:57 AM
Re: How to grep a string in particular colom?
# join -t, -j1 1 -j2 1 -o 2.1 2.2 2.3 file1 file2 > output.txt
...otherwise sort file1 and file2 before using them in a join(1) viz.,
# sort -k1,1 file1 > srtd1
# sort -t, -k1,1 file2 > srtd2
# join -t, -j1 1 -j2 1 -o 2.1 2.2 2.3 srtd1 srtd2 > output.txt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 02:48 PM
12-11-2007 02:48 PM
Re: How to grep a string in particular colom?
while read file1; do
...
done < File1 > output.txt
You can redirect the whole "while" output rather than opening the file once per loop.
>If would be 3 field in File2 then where i have to make changes.
You would have problems because of the quotes. They would have to be removed:
BEGIN { getline; gsub("\"", "", PAT) }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 09:47 PM
12-11-2007 09:47 PM
Re: How to grep a string in particular colom?
Thanks a lot for your valuable replies. I got my answer.
Thanks again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2007 09:48 PM
12-11-2007 09:48 PM
Re: How to grep a string in particular colom?
Thanks a lot for help.