LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-08-2019, 02:21 PM   #1
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Rep: Reputation: Disabled
Question awk/sed help


Hello,

I have a file with 4 fields below. I need to run 'getent passwd uid' on each line and get the full name (gecos) then append it to the file correctly:

uid1 group1 25464.6 44044
uid2 group2 28680.9 60231
uid3 group3 28768.3 50

I can run a loop on uids and getent passwd then awk it to get the gecos but how do I append it back to the original file?

Looking to get this:

"First Last" uid1 group1 25464.6 44044
Jon Doe uid2 group2 28680.9 60231
Jane Doe uid3 group3 28768.3 50
 
Old 03-08-2019, 02:33 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
I know in sed, you use ^ to mean the beginning of a line, so you can do like:
Code:
sed 's/^/new text/' <filename>
But that would work for every line, not just the ones you want. So in this case, I'd guess that instead would be:
Code:
sed 's/$uid1/added text $uid1/'
And have $uid1 be a variable which contains the found UID that you got from each line.
 
Old 03-08-2019, 02:34 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
read
man awk
man sed

Simple example:
Code:
list=("uid1 group1 25464.6 44044"
"uid2 group2 28680.9 60231"
"uid3 group3 28768.3 50")

echo "${list[0]}" | sed 's/^/First Last /'
echo "${list[1]}" | awk '{print "Jon Doe " $0}'
 
Old 03-08-2019, 03:18 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,356
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Which version of AWK do you have? Perhaps you could do something like this with it:

Code:
awk '{cmd="id -u -n " $1; cmd|getline a; print a,$0;}'
That is with the utility id, but hopefully it shows the idea.

As suggested, see "man awk" for the details on the version of AWK you have.
 
Old 03-08-2019, 07:53 PM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,382

Rep: Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761
I would just use a bash script.
Code:
#!/bin/bash

while read u g k l; do
 en=$(getent passwd "$u")
 if [[ "$en" =~ :([[:upper:]][^,]*) ]]; then
  name=${BASH_REMATCH[1]}
  echo "\"$name\" $u $g $k $l"
 else
  echo "No match for $u"
 fi
done < input_file
The pattern match assumes that the full name starts with an upper case character.

Last edited by allend; 03-08-2019 at 07:58 PM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
sed or awk help requested tonyfreeman Programming 7 10-03-2004 12:23 AM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:48 AM.

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