- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk and prinf
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
Discussions
Discussions
Discussions
Forums
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
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
тАО04-26-2010 01:16 AM
тАО04-26-2010 01:16 AM
awk and prinf
I am replacing the 4th feild of an input line and then formatting the input line using printf
Here is my code:
Line="# IN01379 Larry 2010/03/17 (should be the same as FIW-LR AM)"
REPL_DT=`date +'%Y\/%m\/%d'`
REP_LINE=$(echo $Line | awk -v var=$REPL_DT '{$4=var; print}')
NEW_LINE=$(echo $REP_LINE | awk '{printf "# %-7s %9s %16s\n", $2,$3,$4}')
The problem here is, since I am formatting only till $4 in prinf statement, anything that comes after the 4 feild is not printed. The number of feilds/words that comes after 4th feild is not fixed.
I do not have to format the feilds after $4. But I have to format the first four feilds as shown above.
Any way to format only the first 4 feilds of the input line and retain the rest of line.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2010 02:22 AM - edited тАО08-21-2011 03:32 PM
тАО04-26-2010 02:22 AM - edited тАО08-21-2011 03:32 PM
Re: awk and printf
>Anyway to format only the first 4 fields of the input line and retain the rest of line
You could use a for-loop to print the rest of the fields:
for (i=5; i <=NF; ++i)
printf " %s", $I
print ""
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2010 04:20 AM
тАО04-26-2010 04:20 AM
Re: awk and prinf
Another way is to use Perl and do something like:
# echo "abc def ghi jkl xxx yyy zzz"|perl -nae '{printf "# %-7s %9s %16s %s\n",@F[1..3],"@F[4..@F]"}'
Perl counts zero-relative whereas 'awk' counts from one.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2010 08:16 PM
тАО04-26-2010 08:16 PM
Re: awk and prinf
echo "hi hello how are you doing" | awk '{printf "# %-7s %9s %16s %s\n", $2,$3,$4,substr($0,index($0,$5))}'