LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2017, 10:05 PM   #1
mcbobolink
LQ Newbie
 
Registered: Jan 2016
Posts: 1

Rep: Reputation: Disabled
find a field from a directory search & insert that field into second file


hello all,
I have a directory tree of servers that, using grep will show what tier (directory) it was found in. For example:

grep serverA /usr/local/hosts/*/*
=> HT_dev/SUNOS:serverA

The second file I'm using is a scan of those same servers with app versions listed, but not tier info. For example:

serverA,appA 3.2.0,appB 5.0.0,appC 6.2.0
serverB,appA 3.1.5,appB 5.2.0,appC 6.2.0
..
..
serverXYZ123,appA 3.2.0,appB 5.0.0,appC 6.2.0

What I want to accomplish using awk,sed,grep or perl or a combination thereof is to insert the tier info the corresponding record in the second file to be able to sort and plan a multi-tiered upgrade effort. For example:

HT_dev,serverA,appA 3.2.0,appB 5.0.0,appC 6.2.0
HT_ite,serverB,appA 3.1.5,appB 5.2.0,appC 6.2.0
..
..
HT_prod,serverXYZ123,appA 3.2.0,appB 5.0.0,appC 6.2.0

I've tried a few contortions of awk w/ sed, awk w/grep and all have would up with multiple errors. Can anyone point out a solution that has yet eluded me? Thanks for your help.
 
Old 04-26-2017, 05:41 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ!

Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

I find your description a bit confusing.

Do you expect a single result from this?

Code:
grep serverA /usr/local/hosts/*/*
=> HT_dev/SUNOS:serverA
If so, and you want only the first part of the path then you may set it into a variable, like this...

Code:
PREFIX=$(grep serverA /usr/local/hosts/*/*)
...and refer to the first part like this...
${PREFIX%%/*}
And if you are making the change to a single file, your second file, using the same grep term, a simple sed command should do it...

Code:
sed -n s/^serverA,/${PREFIX%%/*},\&/p secondfile
So, given a second file as shown, and the same grep results...

Code:
# cat secondfile
serverA,appA 3.2.0,appB 5.0.0,appC 6.2.0
serverB,appA 3.1.5,appB 5.2.0,appC 6.2.0
serverXYZ123,appA 3.2.0,appB 5.0.0,appC 6.2.0

# echo $PREFIX
HT_dev/SUNOS:serverA

# sed -n s/^serverA,/${PREFIX%%/*},\&/p secondfile
HT_dev,serverA,appA 3.2.0,appB 5.0.0,appC 6.2.0
Adapt that to your own case and let us know if it works for you. The example here will not modify the second file, it will only print out the changed lines to your terminal.

Hope this helps!

Last edited by astrogeek; 04-26-2017 at 06:07 PM. Reason: Added example results, and typo, simpler sed
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Xombrero: can't see text entered in address field and search field xiongnu Linux - Software 1 01-23-2016 08:20 AM
how can I search a file to find if my variable belogs to a field thomai Linux - Newbie 2 05-19-2011 05:54 PM
OpenOffice (insert field) problem. r11_kaede Linux - Software 6 09-14-2008 01:31 PM
Can not insert word file into mysql table (in blob field) prabhatsoni Linux - Software 2 07-21-2006 05:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration