Tag Archives: OFS

OFS does not apply to few records in awk

By chidori

Hi ,

I am having a problem with my awk oneliner , which for some reason leaves the first two records

Input File

Code:

$ cat file1
A1:B1:C1:NoLimit
M1:M2:M3:Limit
A2:B2:C2,C3,C4,C5
A3:B3:C3,C4,C5,C6,C7


Desired output

Code:

A1,B1,C1,NoLimit
M1,M2,M3,Limit
A2,B2,C2
,,,C3
,,,C4
,,,C5
A3,B3,C3
,,,C4
,,,C5
,,,C6
,,,C7


I am using the below oneliner to achieve it partially but i dont get why the first two records OFS does not change to comma
,

Code:

nawk 'BEGIN{FS=":";OFS=","}{gsub(",","n,,,",$NF);print}' file1


This gives me an output

Code:

A1:B1:C1:NoLimit
M1:M2:M3:Limit

A2,B2,C2
,,,C3
,,,C4
,,,C5
A3,B3,C3
,,,C4
,,,C5
,,,C6
,,,C7


Can someone please help me understand the problem and correct the code

Source: FULL ARTICLE at The UNIX and Linux Forums

Pro Athlete Tax Returns Illustrate Complexities of U.S. Tax Code

By Kurt Badenhausen, Forbes Staff

Republican Elephant 2 SC GOP lawmakers blast Labor secretary nominee

This is a guest post from K. Sean Packard, CPA, who is Director of Tax at OFS. He specializes in tax planning and the preparation of tax returns for pro athletes. He can be reached at sean.packard@ofswealth.com and on Twitter at @AthleteTax.

From: http://www.forbes.com/sites/kurtbadenhausen/2013/04/15/pro-athlete-tax-returns-illustrate-complexities-of-u-s-tax-code/

Computing difference based on line contents

By ncwxpanther

I have the following awk script set up to copy the contents of a line that contains 0008 in each line that contains values of 1895 through 2012.

Code:

awk -v OFS=" " '{val=0+substr($1,length($1)-3,4);if(val==0008){print;$1=x;y=$0}else{if(val>=1895&&val<=2012){print $1 y}else{print}}}'


Output data –

Code:

XXXXXXXXX0007 38.3 42.6 51.3 60.1 67.7 75.1 78.4 77.6 71.6 60.2 50.8 42.3 59.7
XXXXXXXXX0008 39.5 43.8 51.7 59.5 67.7 75.2 78.9 77.8 71.7 60.5 50.7 42.6 60.0
XXXXXXXXX1895 39.5 43.8 51.7 59.5 67.7 75.2 78.9 77.8 71.7 60.5 50.7 42.6 60.0
XXXXXXXXX1896 39.5 43.8 51.7 59.5 67.7 75.2 78.9 77.8 71.7 60.5 50.7 42.6 60.0


How would I modify this if I wanted to subtract the values in 0008 from the values in 1895-2012?

Input data –

Code:

XXXXXXXXX0007 38.3 42.6 51.3 60.1 67.7 75.1 78.4 77.6 71.6 60.2 50.8 42.3 59.7
XXXXXXXXX0008 39.5 43.8 51.7 59.5 67.7 75.2 78.9 77.8 71.7 60.5 50.7 42.6 60.0
XXXXXXXXX1895 38.5 33.0 50.0 60.7 67.2 77.2 78.5 79.2 76.8 56.0 50.4 43.4 59.2
XXXXXXXXX1896 41.0 43.9 47.9 66.8 74.2 75.6 79.9 81.5 72.5 60.0 53.7 43.4 61.7


From: http://www.unix.com/shell-programming-scripting/220997-computing-difference-based-line-contents.html

awk comparison with two patterns

By ibanezpete

Here is my list, which contains URLs for file downloads:


//servername.com/version/panasonic1,1_1.1.1
//servername.com/version/panasonic3,1_6.7.1
//servername.com/version/panasonic3,2_6.8
//servername.com/version/panasonic2,6_3.0.2
//servername.com/version/panasonic3,1_7.1.3
//servername.com/version/panasonic2,6_3.0.4

This list been acquired using curl and saved to a text file. The file is used as input for wget and the files are downloaded into a specified directory. I have worked out the curl and wget syntax and have working lines of code.

However I don’t need to download all of the files listed in the text file as some are older versions of software for a particular models of hardware.

From the text above models are defined by model,revision (x,x) and the software versions are in the form major.minor.revision (x.x.x) or sometimes major.minor (x.x).

From my reading of the boards and limited knowledge of awk I need to isolate the (model,revision) patterns


awk ‘BEGIN{FS=”/p”; OFS=”_”}

and use an if statement to print those which are unique. But where there are other lines which match compare by the version numbers, which means I’ll need to redefine FS and OFS to isolate the second pattern.


{FS=OFS=”_”}

The comparison will then only print lines that have the latest software version. From the example lines above that would be:


//servername.com/version/panasonic1,1_1.1.1
//servername.com/version/panasonic3,2_6.8
//servername.com/version/panasonic3,1_7.1.3
//servername.com/version/panasonic2,6_3.0.4

I think awk is up to the task but my knowledge of it is not. Any ideas would be much appreciated!

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Substitute newline with tab at designated field separator

By yifangt

Hello, I need to replace newline with tab at certain lines of the file (every four lines is a record).

Code:

infile.fq:

@GAIIX-300
ATAGTCAAAT
+
_SZS^\cd
@GAIIX-300
CATACGACAT
+
hhghfdffhh
@GAIIX-300
GACGACGTAT
+
gggfc[hh]f


Code:

outfile:

@GAIIX-300 ATAGTCAAAT + _SZS^\cd
@GAIIX-300 CATACGACAT + hhghfdffhh
@GAIIX-300 GACGACGTAT + gggfc[hh]f


I used

Code:

sed '/^@/N;/^@/N;/^@/N;s/n/t/g' infile.fq


without full understanding. I figured out this oneliner when I tried to understand newline substitution in sed.
1) Can anyone explain the four consecutive /^@/N for me?
2) How can I use another command tr to do the job like:

Code:

!n@ | tr 'n' 't' outfile.tab


by adding the condition !n@ to filter the record separator, which is @ here? I know awk can do the job much easier with RS=”@”, OFS=”t”,

Code:

awk 'BEGIN{RS="@"; OFS="t"} {print $1, $2, $3, $4}' infile.fq


but I want to understand how sed and tr work, if they can, in this case.
Thanks a lot!
YF

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

How to compare 2 files column's more than 5?

By Akshay Hegde

Hi All I am just trying to compare 2 file using column information using following code

Code:

awk '
NR==FNR {A[$1,$2,$3,$4,$5,$6,$7,$8]=$9; next}
{B=A[$1,$2,$3,$4,$5,$6,$7,$8]; print $0,B""?B:" Not -In file" }
' OFS="t" file1 file2


if file1 matches with file2 then print

Code:

$9


content in file 1 along with file2

Code:

$0


suppose if I keyed on only

Code:

$1


in array then I could able to see result but I want to compare at least 5 column out of 8

Those who know please help

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Match first column and separate entries

By kaav06

Hi

I have 2 big files containing following information:

file 1

Code:

12345

345634

217341

87234693

8236493


file 2:

Code:

12345 1237
732432 6459
345634 3456
344545 23423
217341 342643
87234693 34345
8236493 342332


Expected out put is to match first column of 2 files and fetch the number sin front of it in second file

Code:

12345 1237
345634 3456
217341 342643
87234693 34345
8236493 342332


I am using following code but didnt work

Code:

awk '{ gsub(/r/, "") gsub(/ *t/, "t")} FNR == NR {f[$1] next} { for(n = split($1, f1, ","); n > 0; n--) if(f1[n] in f) { $1 = f1[n] print }} file1 FS="t" OFS="t" file 2


Kindly let me know the scripting to sove the issue

K

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

How to take awk result out (piping to other program)?

By Akshay Hegde

Hi! all here is my code
which is working fine no errors but I want to know how to take result and input to other program

Code:

awk 'FNR==1{i++}{LC[i]=NR}
{for(k=1; k<=NF; k++) A[i,FNR,k]=$k}
END{for (i=1;i<=LC[1];i++)
{
for(j=1;j<=LC[2];j++)
if(A[2,j,8]=='$UID' && A[2,j,4]>='$MX'+A[1,i,3] && A[2,j,4]<='$MIN'+A[1,i,4] &&
A[2,j,4]>='$MN'+A[1,i,1] && A[2,j,5]<='$MX'+A[1,i,2])
print A[2,j,6],A[2,j,7]
}}' OFS="t" STD_FIILE FILE2 | program(print) command -XA[2,j,5] -Y[2,j,5] -------------> if statement is true


if if statement is awk program in false

then

Code:

OFS="t" STD_FIILE FILE2 | program(mask) command -XA[2,j,5] -Y[2,j,5] ------------->


please help me

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

awk Merging multiple files with symbol representing new file

By Akshay Hegde

I just tried following

Code:

ls *.dat|sort -t"_" -k2n,2|while read f1 && read f2; do
awk '{print}' $f1
awk FNR==1'{print $1,$2,$3,$4,$5,"*","*","*" }' OFS="t" $f2
awk '{print}' $f2
done


got following result

Code:

18-Dec-1983 11:45:00 AM 18.692 84.672 0 25.4 24
18-Dec-1983 11:45:00 AM 18.692 84.672 5 25.5 24
18-Dec-1983 11:45:00 AM 18.692 84.672 10 26 24
18-Dec-1983 03:22:00 PM 18.327 84.29 * * *
18-Dec-1983 03:22:00 PM 18.327 84.29 0 24.5 24
18-Dec-1983 03:22:00 PM 18.327 84.29 5 24.6 24
18-Dec-1983 03:22:00 PM 18.327 84.29 10 24.8 24
18-Dec-1983 03:22:00 PM 18.327 84.29 40 27.1 24
18-Dec-1983 03:22:00 PM 18.327 84.29 45 27 24
18-Dec-1983 06:45:00 PM 18.025 83.962 0 26.4 23 -- > before this line * symbol suppose to come
18-Dec-1983 06:45:00 PM 18.025 83.962 5 26.5 23
18-Dec-1983 06:45:00 PM 18.025 83.962 10 26.5 23
18-Dec-1983 06:45:00 PM 18.025 83.962 15 26.8 23


Can anyone tell me why it’s not coming and what’s solution ?

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Can anyone tell me what's wrong with my script

By Akshay Hegde

here is my script…stored file1 data in array and comparing with second file..result is coming but wrong…in a sense..if I remove 1st line where I stored data in array, and store instead in some bash variable like

Code:

x=`{awk 'FNR{print $1}}' file1`


then result comes properly,but execution is very slow…

Code:

c=`awk 'END{print NR}'file1`
awk '{{getline< "file1";ln[NR]=$1; lt[NR]=$2}}
FNR<=NR{
l1=$1;
la1=$2;
p=la1+0.1;
m=la1-0.1;
x=0;
y=0;
{
for(j=1;j<='$c';j++)
if(lt[j]>=m && lt[j]<=p)
{
if(l1>=ln[j])
x=x+1
if(l1<=ln[j])
y=y+1
}
}
if(x>=1 && y>=1)
print l1,la1,"Wow you are safe..."
}' OFS="t" file2.dat


if anyone can tell where I am doing wrong, It will be helpful…

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

AIX pam ssh/sshd configuration not allowing sed or awk

By pjeedu2247

This is a weird problem. Following is my code.

Code:

/opt/quest/bin/vastool configure pam sshd
/opt/quest/bin/vastool configure pam ssh

cat /etc/pam.conf |
awk '$1=="ssh"||$1=="sshd"||$1=="emagent"{sub("prohibit","aix",$NF);}1' OFS='t' > /etc/pam.conf

cat /etc/ssh/sshd_config |
sed -e 's/^UsePAM no/UsePAM yes/' > /etc/ssh/sshd_config


If I remove below from the script, both awk and sed are working accordingly.

Code:

/opt/quest/bin/vastool configure pam sshd
/opt/quest/bin/vastool configure pam ssh


But if I execute them together, those files, which are supposed to change are becoming empty :confused::confused:

Is there any relation between pam ssh configuration and sed/awk? I totally not getting the point behind that.

Source: FULL ARTICLE at The UNIX and Linux Forums

Update specific field in a line of text file

By bmtoan

I have a text file like this:

subject1:LecturerA:10

subject2:LecturerA:40

if I was given string in column 1 and 2 (which are subject 1 and LecturerA) , i need to update 3rd field of that line containing that given string , which is, number 10 need to be updated to 100 ,for example.

The following works:
awk -F: ‘{if($1~sub&&$2~lec){$3=newMark}}1’ sub=”subject1″ lec=”LecturerA” newMark=100 OFS=”:” test.txt

But if I want to pass sub and lec to another variable from user input , it does not work:

sb=”subject1″
lect=”LecturerA”
mark=100

awk -F: ‘{if($1~sub&&$2~lec){$3=newMark}}1’ sub=$sb lec=$lect newMark=$mark OFS=”:” test.txt

any idea?
Thanks.

Source: FULL ARTICLE at The UNIX and Linux Forums