Using AWK: If value exists, assign to variable

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Really sorry if this has been asked before as it feels basic, I'm just not connecting the dots and it's making me tear my hair out!
Essentially I have a text file , and I'm assigning the second field of particular rows to a variable i.e
VAR=$(awk 'NR==1 print $2' entry.txt)
However, sometimes on row one in the text file, it will have 4 fields. I would like to say
If ROW 1 has 4 fields
VAR = $2, $3 & $4 concatenated
Else
VAR = $2
I'm sure this is basic awk, I'm just undertaking a small project and have never used it before. I know i should read a book, but this really is a tiny script!
Thanks
awk
add a comment |Â
up vote
0
down vote
favorite
Really sorry if this has been asked before as it feels basic, I'm just not connecting the dots and it's making me tear my hair out!
Essentially I have a text file , and I'm assigning the second field of particular rows to a variable i.e
VAR=$(awk 'NR==1 print $2' entry.txt)
However, sometimes on row one in the text file, it will have 4 fields. I would like to say
If ROW 1 has 4 fields
VAR = $2, $3 & $4 concatenated
Else
VAR = $2
I'm sure this is basic awk, I'm just undertaking a small project and have never used it before. I know i should read a book, but this really is a tiny script!
Thanks
awk
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Really sorry if this has been asked before as it feels basic, I'm just not connecting the dots and it's making me tear my hair out!
Essentially I have a text file , and I'm assigning the second field of particular rows to a variable i.e
VAR=$(awk 'NR==1 print $2' entry.txt)
However, sometimes on row one in the text file, it will have 4 fields. I would like to say
If ROW 1 has 4 fields
VAR = $2, $3 & $4 concatenated
Else
VAR = $2
I'm sure this is basic awk, I'm just undertaking a small project and have never used it before. I know i should read a book, but this really is a tiny script!
Thanks
awk
Really sorry if this has been asked before as it feels basic, I'm just not connecting the dots and it's making me tear my hair out!
Essentially I have a text file , and I'm assigning the second field of particular rows to a variable i.e
VAR=$(awk 'NR==1 print $2' entry.txt)
However, sometimes on row one in the text file, it will have 4 fields. I would like to say
If ROW 1 has 4 fields
VAR = $2, $3 & $4 concatenated
Else
VAR = $2
I'm sure this is basic awk, I'm just undertaking a small project and have never used it before. I know i should read a book, but this really is a tiny script!
Thanks
awk
asked Apr 20 at 15:48
Harry S
31
31
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file
This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.
With different logic:
awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file
Either of these commands goes inside your command substitution:
var=$( awk ... )
Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
â Harry S
Apr 23 at 15:31
add a comment |Â
up vote
0
down vote
awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input
Thank you, that was very helpful in understanding how to apply the problem to any row of a file
â Harry S
Apr 23 at 15:32
add a comment |Â
up vote
0
down vote
You can do
awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile
The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file
This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.
With different logic:
awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file
Either of these commands goes inside your command substitution:
var=$( awk ... )
Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
â Harry S
Apr 23 at 15:31
add a comment |Â
up vote
1
down vote
accepted
awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file
This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.
With different logic:
awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file
Either of these commands goes inside your command substitution:
var=$( awk ... )
Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
â Harry S
Apr 23 at 15:31
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file
This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.
With different logic:
awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file
Either of these commands goes inside your command substitution:
var=$( awk ... )
awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file
This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.
With different logic:
awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file
Either of these commands goes inside your command substitution:
var=$( awk ... )
edited Apr 23 at 15:43
answered Apr 20 at 15:52
Kusalananda
102k13199315
102k13199315
Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
â Harry S
Apr 23 at 15:31
add a comment |Â
Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
â Harry S
Apr 23 at 15:31
Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
â Harry S
Apr 23 at 15:31
Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
â Harry S
Apr 23 at 15:31
add a comment |Â
up vote
0
down vote
awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input
Thank you, that was very helpful in understanding how to apply the problem to any row of a file
â Harry S
Apr 23 at 15:32
add a comment |Â
up vote
0
down vote
awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input
Thank you, that was very helpful in understanding how to apply the problem to any row of a file
â Harry S
Apr 23 at 15:32
add a comment |Â
up vote
0
down vote
up vote
0
down vote
awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input
awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input
answered Apr 20 at 15:52
DopeGhoti
40k54779
40k54779
Thank you, that was very helpful in understanding how to apply the problem to any row of a file
â Harry S
Apr 23 at 15:32
add a comment |Â
Thank you, that was very helpful in understanding how to apply the problem to any row of a file
â Harry S
Apr 23 at 15:32
Thank you, that was very helpful in understanding how to apply the problem to any row of a file
â Harry S
Apr 23 at 15:32
Thank you, that was very helpful in understanding how to apply the problem to any row of a file
â Harry S
Apr 23 at 15:32
add a comment |Â
up vote
0
down vote
You can do
awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile
The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.
add a comment |Â
up vote
0
down vote
You can do
awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile
The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can do
awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile
The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.
You can do
awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile
The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.
edited Apr 23 at 15:46
answered Apr 20 at 15:52
ñÃÂsýù÷
14.8k82462
14.8k82462
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438972%2fusing-awk-if-value-exists-assign-to-variable%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password