- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- AWK script
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
11-24-2002 02:31 AM
11-24-2002 02:31 AM
I have an AWK script written below to change the 2nd entry in a text file. The text file has the following structure:
#cat myFile.txt
bill:g68hjdh78gh:100:88
harry:bft647hsds:7876:77
... (the list continues)
The script is written as follows:
#cat awk_test.sh
#!/bin/sh
awk < myFile.txt
'BEGIN{
FS = ":"
}
{
split($0, field, ":") #split fields delimited by :
if(field[1] ~ /^bill$/)
{
field[2]=`head -1 pattern`
}
}
END{
print field[1]
print field[2]
}
I executed the script as:
# awk_test.sh
However, the 2nd field was not at all modified with the new value specified from the file "pattern".
#cat pattern
dhsgh777
Also I get an error message for the line
field[2]=`head -1 pattern`, which says:
awk_test.sh: = dhsgh777: command not found
Could someone show me how I could modify the script to solve this error and at the same time modify the 2nd field in the file "myFile.txt" ?
I'm a newbie in AWK scripting.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2002 02:48 AM
11-24-2002 02:48 AM
Re: AWK script
The second field in the passwd file is encrypted !!!!! it is not possible to change this by replacing this field with awk or sed or wathever filter. Think you have better luck using the command
#passwd user
Robert-Jan.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2002 03:15 AM
11-24-2002 03:15 AM
SolutionTake look at this link, it will help you.
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xce0857bd90a9d611abdb0090277a778c,00.html
Kind regards,
Robert-Jan.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2002 06:44 AM
11-24-2002 06:44 AM
Re: AWK script
Assuming you'd like to print the first two fields for everyline (after your modifications). I'd do something like this:
awk -F: -v var=$(head -1 pattern) < myFile.txt '{
if ( $1 == "bill" ) $2 = var;
printf("%s %s\n",$1,$2);
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2002 12:10 PM
11-24-2002 12:10 PM
Re: AWK script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2002 02:16 AM
11-25-2002 02:16 AM
Re: AWK script
awk 'BEGIN { FS = ":" }
{ split($0, field, ":")
if ( field[1] ~ /^bill/ )
{
cmd = "head -1 pattern"
cmd | getline output
close(cmd)
field[2] = output
print field[1], field[2]
}
}' myFile.txt
It makes use of one line command substitution using a pipe. If you have the Oreilly Sed and Awk book, this is well documented with good examples.
Cheers,
Joseph