- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Awk inside for loop
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
07-06-2005 09:53 PM
07-06-2005 09:53 PM
I have a text file that look like this:
20050605
00 qq
01 ss
02 aa
03 rr
Now, i want to make it in this format...
20050605 00 qq
20050605 01 ss
20050605 02 aa
20050605 03 rr
using awk i was able to produce it:
awk '
NF == 1 { tab = $1 ; next }
NF == 2 { print tab , $0 } ' filename
but when i put inside a loop im only getting this result:
00 qq
01 ss
02 aa
03 rr
the other field is gone..i need to run it inside a for loop since i have mutiple files to convert and this is what i used:
for sgry in *.log
do
awk '
NF == 1 { tab = $1 ; next }
NF == 2 { print tab , $0 } ' $sgry > new.tmp
mv new.tmp $sgry
done
is there a better way to do it?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 10:13 PM
07-06-2005 10:13 PM
Re: Awk inside for loop
# awk '/20050605/ { var=$1; getline;getline;} { print var" "$0 }' testfile
20050605 00 qq
20050605 01 ss
20050605 02 aa
20050605 03 rr
getline;getline is used to move 20050605,' ' line.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 10:17 PM
07-06-2005 10:17 PM
Re: Awk inside for loop
# Script Starts here
####################
exec 4<$1
read -ru4 first
while read -ru4 f1 f2
do
echo $first $f1 $f2
done
# Script Ends Here
###################
Store above script in a file "myscript"
and run at commandline as:
# ./myscript testfile_name
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 10:23 PM
07-06-2005 10:23 PM
Re: Awk inside for loop
for file in `ls *.log`
do
awk '/20050605/ { var=$1; getline;getline;} { print var" "$0 }' $file > tmpfile
mv tmpfile $file
done
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 10:26 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 10:33 PM
07-06-2005 10:33 PM
Re: Awk inside for loop
Can you try like this?
ls *.log >test
for sgry in `cat test`
do
awk '
NF == 1 { tab = $1 ; next }
NF == 2 { print tab , $0 } ' $sgry > new.tmp
mv new.tmp $sgry
done
Cheers
Shyjith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 10:41 PM
07-06-2005 10:41 PM
Re: Awk inside for loop
yet another solution
awk 'BEGIN { getline; var=$0 ; getline } { print var" "$0 }' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 10:44 PM
07-06-2005 10:44 PM
Re: Awk inside for loop
The 20050607 is actually the current date yyyymmdd, can anybody show me how to make my output like this:
Jun07 00 aa
Jun07 01 bb
Jun07 02 cc
Jun07 03 dd
Jun07 04 ee
tnx...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 10:51 PM
07-06-2005 10:51 PM
Re: Awk inside for loop
for file in *.log
do
awk '{ if ( $0 != "" && NF != 1 ) { print var" "$0 }}' var="Jul 07" $file > tmpfile
mv tmpfile $file
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 11:04 PM
07-06-2005 11:04 PM
Re: Awk inside for loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2005 11:11 PM
07-06-2005 11:11 PM
Re: Awk inside for loop
you can easily do it as,
## CODE ###
for file in *.log
do
awk '{ if ( $0 != "" && NF != 1 ) { print var" "$0 }}' var=`date +'%a%d'` $file > tmpfile
mv tmpfile $file
done