Sponsored Content
Top Forums Shell Programming and Scripting Search pattern then find and replace Post 302986075 by RavinderSingh13 on Saturday 19th of November 2016 03:36:01 PM
Old 11-19-2016
Quote:
Originally Posted by Joselouis
If condition satisfy, want to find pattern and modify two Fields in Modify.txt

Input.txt
Code:
SOURCE1
SOURCE2
SOURCE3
SOURCE4
SOURCE5
SOURCE6

Modify.txt
Code:
SOURCE1|SLA|2016/12/11 11:12:11 PM|HMM|11-11-16|
SOURCE2|SLA|2016/13/11 11:12:11 PM|HMM|10-11-16|
SOURCE3|SLA|2016/14/11 11:12:11 PM|HMM|16-11-16|
SOURCE4|SLA|2016/15/11 11:12:11 PM|HMM|14-11-16|
SOURCE5|SLA|2016/16/11 11:12:11 PM|HMM|13-11-16|
SOURCE6|SLA|2016/17/11 11:12:11 PM|HMM|12-11-16|
SOURCE7|SLA|2016/18/11 11:12:11 PM|HMM|02-11-16|
SOURCE8|SLA|2016/19/11 11:12:11 PM|HMM|01-11-16|

Code:
cat Input.txt | while read line 
do
   VAR1=$(Operation with Input.txt file)#VAR1 has:YYYY/MM/DD HH:MM:SS
   VAR2=$(Operation with Input.txt file)#VAR2 has: DD-MM-YY
   if [[ -z $VAR1 && -z VAR2 ]];then 
      awk -F"|" '$1~/$line/{gsub("$VAR1", $3)}1;{gsub("VAR2",$5) ' Mofidy.txt
done

If VAR1 and VAR2 is not empty, in above awk trying to search a line in Modify.txt file and replace filed 3 and 5 with $VAR1 and $VAR2. but it is not working
Code:
Expected output:
SOURCE1|SLA|2016/12/11 11:12:11 PM|HMM|11-11-16|
SOURCE2|SLA|2016/13/19 11:12:11 PM|HMM|20-11-16|
SOURCE3|SLA|2016/14/11 11:12:11 PM|HMM|16-11-16|
SOURCE4|SLA|2016/15/11 11:12:11 PM|HMM|14-11-16|
SOURCE5|SLA|2016/16/11 11:12:11 PM|HMM|13-11-16|
SOURCE6|SLA|2016/17/11 11:12:11 PM|HMM|12-11-16|
SOURCE7|SLA|2016/18/11 11:12:11 PM|HMM|02-11-16|
SOURCE8|SLA|2016/21/11 11:12:11 PM|HMM|29-11-16|

Hello Joselouis,

If you want to do a check on a variable's value either it is NULL or not then -z checks if a variable is empty if block's value will be TRUE so either you could use your awk into else part or you could use -n option to test variable values.
Following may help you in same.
Code:
if [[ -n $VAR1 && -n $VAR2 ]]
then
    awk -vvar1="$VAR1" -vvar2="$VAR2" 'FNR==NR{A[$1]=$1;next} ($1 in A){$3=var1;$5=var2;print}' input.txt FS="|" OFS="|" modify.txt
else
    echo "VAR1 and VAR2 both doesn't have values."
fi

Adding a one liner form of solution too here.
Code:
if [[ -n $VAR1 && -n $VAR2 ]]; then awk -vvar1="$VAR1" -vvar2="$VAR2" 'FNR==NR{A[$1]=$1;next} ($1 in A){$3=var1;$5=var2;print}' input.txt FS="|" OFS="|" modify.txt; else echo "VAR1 and VAR2 both doesn't have values."; fi

Let us know how it goes then, I hope this helps you.

Quote:
try with this code


Code:
while read line
do
VAR1=$(Operation with Input.txt file)#VAR1 has:YYYY/MM/DD HH:MM:SS
VAR2=$(Operation with Input.txt file)#VAR2 has: DD-MM-YY
if [[ -z ${VAR1} && -z ${VAR2} ]];then
awk -F"|" '$1~/$line/{sub(V1,$3)}1;{sub(V2,$5)' V1="${VAR1}" V2="${VAR2}" Mofidy.txt
fi
done < Input.txt
Hello itkamraj,

As I have mentioned above in your suggestion IMHO we could change -z to -n and make a smaller change in sub syntax, though I haven't tested it at all.
Code:
while read line 
do
   VAR1=$(Operation with Input.txt file)#VAR1 has:YYYY/MM/DD HH:MM:SS
   VAR2=$(Operation with Input.txt file)#VAR2 has: DD-MM-YY
   if [[ -n ${VAR1} && -n ${VAR2} ]];then 
      awk -F"|" '$1~/$line/{sub(V1,$3)};1;{sub(V2,$5)}' V1="${VAR1}" V2="${VAR2}" modify.txt
   fi
done < Input.txt

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a Pattern and Replace

Hello All, Can you help me with this , I need to search a pattern replace it with the new pattern in all the files in a directory. what would be the easiest way to do that? Thanks in advance. :) Sam, (6 Replies)
Discussion started by: sbasetty
6 Replies

2. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

3. Shell Programming and Scripting

search replace pattern containing slashes

Need help in finding pattern then replacing pattern that contains multiple slashes .. ex . <imgp src="Attention_web.eps.jpg" align="left"> <imgp src="NewToday062308.eps.jpg"> replace with <imgp src="/ww2/adpay/liner/Attention_web.eps.jpg" align="left"> <imgp... (2 Replies)
Discussion started by: aveitas
2 Replies

4. Shell Programming and Scripting

search a pattern and replace the whole line

Hi All, I have a requirement where I have to find a pattern in a file and comment the whole line containing the search pattern. Any ideas in shell is welcome. Thanks in advance, Regards, Arun (3 Replies)
Discussion started by: arun_maffy
3 Replies

5. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

6. UNIX for Dummies Questions & Answers

pattern containg ' search and replace

Hi guys I'm new to this forum so please help me in this I have a file where i need to replace a pattern value=' ' with the pattern value='abc' and moreover that abc value must be passed from some variable say i assign name=abc and use name as the value to replace instead of the direct string... (10 Replies)
Discussion started by: sundarj
10 Replies

7. Shell Programming and Scripting

Search and replace - pattern-based

Hey folks! I am new to shell-scripting, but I have a problem that I would like to solve using a script. I create very large html forms, used for randomized trials. In these forms, each question is supplied with a variable that looks something like this: PROJECT_formNN Where NN is the question... (1 Reply)
Discussion started by: Roevhat
1 Replies

8. Shell Programming and Scripting

Search for a pattern and replace. Help needed

I have three variables $a, $b and $c $a = file_abc_123.txt $b = 123 $c = 100 I want to search if $b is present in $a. If it is present, then i want to replace that portion by $c. Here $b = 123 is present in "file_abc_123.txt", so i need the output as "file_abc_100.txt' How can this be... (3 Replies)
Discussion started by: irudayaraj
3 Replies

9. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

10. Shell Programming and Scripting

sed - Search and replace within pattern

Hi Guys! Unix newbie here! Have a requirement for which I have been scouting the forums for a solution but has been out of luck so far :( I have a file which contains the following:- TEST1|TEST2|"TEST3|1@!2"|TEST5 My sed command should result in either one the following output:-... (6 Replies)
Discussion started by: hishamzz
6 Replies
All times are GMT -4. The time now is 09:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy