LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-25-2022, 04:00 PM   #1
klmcguire
LQ Newbie
 
Registered: Feb 2022
Posts: 9

Rep: Reputation: 0
Creating Tab Spacing Between Two Values in AWK Command


First, if there is a specific way to insert code in these questions, let me
know. Sorry if I did it wrong on my first post.

I am trying to format spacing in my awk command shell script. In the for loop below, I am pulling two values from two specific columns and a specific row number, from a separate file. All of that works, I just need to create tab spacing between the two values, $3 and $4. How is this done?

for ((i=$START;i<=$END;i++))
do
awk -v i="$i" 'FNR == 46 {print "Complex "i" "$3" "$4" \n\n"}' mmpbsa_ab$i.log >> MMPBSA-Energies.dat
sleep 3;
done
 
Old 02-25-2022, 04:34 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,573

Rep: Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534Reputation: 2534
Quote:
Originally Posted by klmcguire View Post
First, if there is a specific way to insert code in these questions, let me
know.
It's "[code]..[/code]" - see https://www.linuxquestions.org/quest....php?do=bbcode


Quote:
I just need to create tab spacing between the two values, $3 and $4. How is this done?
"\t" is the escape for tab character, so simply "print $3"\t"$4" will do that.

Alternatively, if you want it between all fields, you could set Awk's output field separator (OFS) variable instead, e.g:
Code:
$ echo 'a b' | awk 'BEGIN{OFS="\t"} {print "prefix",$1,$2,"suffix"}' | cat -A
prefix^Ia^Ib^Isuffix$
(Where "cat -A" renders tabs as "^I")


Last edited by boughtonp; 02-25-2022 at 04:35 PM.
 
Old 02-25-2022, 04:42 PM   #3
klmcguire
LQ Newbie
 
Registered: Feb 2022
Posts: 9

Original Poster
Rep: Reputation: 0
Awesome! The first example works great. Out of curiosity, instead of a tab space, can you define any amount of space, say 2 or 3 or even 10, etc?
 
Old 02-26-2022, 03:57 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
You can set OFS to any value you see fit, see "man awk" for the details, but it would be the same between each of the fields printed out when printed as a whole line.

There are different versions of AWK so you will need to check, but most or all of them have a printf() function. That works like any other printf() and you can use it to provide leading or trailing spaces to pad out a field or fields. But then you'd have to enumerate the fields printed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The first browser to introduce a second level in the tab bar for managing tab groups: Two-Level Tab LXer Syndicated Linux News 0 03-01-2021 05:50 AM
Tab spacing issues George.evans Linux - Newbie 2 11-30-2012 10:46 AM
[SOLVED] Why is my awk script producing extra spacing between the first and second column? mystupidquestion Programming 2 10-09-2011 12:51 PM
LXer: Double Spacing In Awk, Perl and Shell on Linux and Unix LXer Syndicated Linux News 0 07-08-2008 06:41 AM
Problem with frames in GTK+ - no spacing between frame and text entry Nylex Programming 8 03-01-2006 01:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:36 PM.

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