file content as a part of an if-statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file content as a part of an if-statement
# 1  
Old 01-20-2011
file content as a part of an if-statement

Hello,
I have following problem.
I have the result of a database request. I preparated the result via sed, etc. as a string in a file.
The string in the file is:
($3==1 || $3==2 || $3==3 || $3==4)
Now I want to use the String as a command in an if-statement.
So I assigned the string to a variable:
ABC=$(<dir/file)

So far - so good:
echo $ABC gives me as a result: ($3==1 || $3==2 || $3==3 || $3==4)

But with nawk it doesn't work.

For clarification: If I would use the string directly (and so static), my awk would be written like here:

#If we find the values 1,2,3 or 4 in column 3, so change the value in column 4 into 10 in the relevant rows.
nawk '{FS=";";OFS=";"; if ($3==1 || $3==2 || $3==3|| $3==4) {$5=10}; print $0}' dir/file1.txt > dir/file2.txt

But dynamically it doesn't work. My idea was following:

nawk '{FS=";";OFS=";"; if ($ABC) {$5=10}; print $0}' dir/file1.txt > dir/file2.txt

I tested everything, with echo $ABC, cat, different brackets... but without success.

Is anybody able to help me and explain to me what I have to change, please?

Thanks a lot and Kind Regards
Dr_Aleman
# 2  
Old 01-20-2011
Can you try by assigning $ABC to an awk variable..
Code:
nawk -v var="$ABC" '{FS=";";OFS=";"; if (var) {$5=10}; print $0}' dir/file1.txt > dir/file2.txt

or directly w/o awk variable
Code:
nawk '{FS=";";OFS=";"; if ('"$ABC"') {$5=10}; print $0}' dir/file1.txt > dir/file2.txt


Last edited by michaelrozar17; 01-20-2011 at 08:50 AM..
This User Gave Thanks to michaelrozar17 For This Post:
# 3  
Old 01-20-2011
Use -v option with awk.

Code:
nawk -v var=$ABC 'BEGIN{FS=";";OFS=";"} {if (var) {$5=10}; print $0}' dir/file1.txt > dir/file2.txt

This User Gave Thanks to pravin27 For This Post:
# 4  
Old 01-20-2011
MySQL

Thank you guys for the quick answers.
@Pravin27: With your solution I got an error after execution:

Code:
 
nawk: syntax error at source line 1
 context is
         >>> || <<<
nawk: bailing out at source line 1

@Michaelrozar: your first suggestion changed all of the values in the column into 10 (not only the relevant rows with the 1,2,3 and 4).
But your second suggestion worked perfectly:

Code:
nawk '{FS=";";OFS=";"; if ('"$ABC"') {$5=10}; print $0}' dir/file1.txt > dir/file2.txt

Thanks a lot and have a nice Day!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

Insert content of a file to another file at a line number which is given by third file

Hi friends, here is my problem. I have three files like this.. cat file1.txt ======= unix is best unix is best linux is best unix is best linux is best linux is best unix is best unix is best cat file2.txt ======== Windows performs better Mac OS performs better Windows... (4 Replies)
Discussion started by: Jagadeesh Kumar
4 Replies

3. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

4. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

6. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

7. Programming

PASSING PART OF FILE CONTENT TO VARIABLE

All, I have a log file containing lots of data now i want to extract all text between block below(names) without the title or end pattern but only names, ++++START++++ SCOTT TIGER HENRY PAUL JARED OTIENO OMOLLO JA NIGERIA ++++END++++ the names i want to return and store in a variable in... (1 Reply)
Discussion started by: Scott2000
1 Replies

8. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

9. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

10. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies
Login or Register to Ask a Question