- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: String formatting
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
тАО05-01-2008 07:36 PM
тАО05-01-2008 07:36 PM
String formatting
For eg)
awk 'BEGIN { printf ("%s %-51s %s %-7s %s",$var1,$var2,$var3,$var4,$var5)}'
I tried to the above and got the error message.
Is there any other way to format strings so that my purpose of writing the whole string into the file will be successful.
Please help
Regards
rkraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2008 08:07 PM
тАО05-01-2008 08:07 PM
Re: String formatting
"UNIX" is not a very well-defined quantity.
man printf
td192> printf ">%10s< >%-10s<\n" 'aaa' 'bbb'
> aaa< >bbb <
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2008 12:46 AM
тАО05-02-2008 12:46 AM
Re: String formatting
there is a 'printf' binary, which you can use in shell scripts, like:
printf "%02d %32s\n" $i $text
In awk, your syntax needs modification: drop the "$" before variable names.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2008 01:05 AM
тАО05-02-2008 01:05 AM
Re: String formatting
If you still want to go the awk route, you need to know how to pass variables. (It would be helpful if you printed the error message.)
Your issue is that you have $var1 within single quotes, where variables aren't expanded and in awk $var1 basically prints field ($) var1, where var1 is likely 0.
To pass variables, you either need to do stuttering quoting or:
awk -v var1="$var1" -v var2="$var2" ... '
BEGIN {
printf "%s ....\n", var1, var2, var3, var4, var5
exit
} '
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2008 03:42 AM
тАО05-02-2008 03:42 AM
Re: String formatting
And for completeness in the discussion, 'printf' is also intrinsic to Perl. So you can write in C, shell, awk or Perl as best suits your needs and those of your intended maintainers.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2008 07:58 AM
тАО05-02-2008 07:58 AM
Re: String formatting
Also i tried this way
var1="CTCT 0001000Y"
printf '%-50s' $var1
CTCT 0001000Y $
A single space is there between CTCT and 0001000Y and that is getting expanded while i am using the formatted one.
if var1="CTCT" then it works fine as expected.
Any clues
let me know
Regards
rkraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2008 08:09 AM
тАО05-02-2008 08:09 AM
Re: String formatting
I think, it is all done correctly: You are using an unquoted variable, so contained whitespace leads to mutliple arguments of your command.
Look at the difference of your
var1="CTCT 0001000Y"
printf '%-50s\n' $var1
to a double quoted version
printf '%-50s\n' "$var1"
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2008 08:11 AM
тАО05-02-2008 08:11 AM
Re: String formatting
> Also i tried this way
> var1="CTCT 0001000Y"
> printf '%-50s' $var1
> CTCT 0001000Y $
> A single space is there between CTCT and 0001000Y and that is getting expanded while i am using the formatted one.
if var1="CTCT" then it works fine as expected.
> Any clues
I'm not sure what it is you are asking.
First, you probably want to include a newline when you print. Second, if your variable has embedded spaces, but you want to treat that as *one* argument to 'printf', then you need to quote the variable argument:
# printf '%-50s\n' "$var1"
CTCT 0001000Y
Otherwise, something like this:
# printf '%-50s\n' $var1 and yet more here
...becomes:
CTCT
0001000Y
and
yet
more
here
...instead of:
# printf '%-50s\n' "$var1 and yet more here"
CTCT 0001000Y and yet more here
Regards!
...JRF...