Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Replace first occurrence of a string in while loop


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Replace first occurrence of a string in while loop

####Solved####
Hello,
My aim is to replace searched string with incremented value under ubuntu 16.04.
Example:
Code:
aasasasas 9030 31wwo weopwoep
weerasas 9030 ew31wo ieopwoep
bbqqqsas 9030 ew3swo ieeopwoep
ccsaqpas 9030 ewiro o2opwoep

Expected:
Code:
aasasasas 9030 31wwo weopwoep
weerasas 9031 ew31wo ieopwoep
bbqqqsas 9032 ew3swo ieeopwoep
ccsaqpas 9033 ewiro o2opwoep

I know, what I tried is logically not true:
Code:
i=9029
while read -r line
do
i=$i++
sed -i '0,/9030/{s/9030/$i/}' example 
done<example > expected

Is it possible to get expected output with sed?

EDIT: I tried below command but it starts from 1:
Code:
cat example |  awk '/9030/{count++; { sub("9030",count, $0)}; }; {print }'


Final Edit:
Code:
cat example |  awk '/9030/{count++; { sub("9030",count+9029, $0)}; }; {print }'

I'd appreciate you help
Thank you
Boris

Last edited by baris35; 05-08-2019 at 10:36 PM.. Reason: solved
This User Gave Thanks to baris35 For This Post:
# 2  
Hello baris35,

Very good that you have solved your question by yourself and appreciate that you have shared with us too, keep it up Smilie
You could better your solution by doing this:
Code:
awk '{sub("9030",$2+count++,$2)} 1'   Input_file

OR
Code:
awk '$2==9030{$2=$2+count++} 1'  Input_file

Output will be as follows.
Code:
aasasasas 9030 31wwo weopwoep
weerasas 9031 ew31wo ieopwoep
bbqqqsas 9032 ew3swo ieeopwoep
ccsaqpas 9033 ewiro o2opwoep

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
$2==9030 is quite precise, compared to /9030/ where 9030 can appear as string or substring in all columns.
Back to the while loop. The read command can split the columns into variables
Code:
i=9029
while read -r col1 col2 remainder
do
  i=$(( i+1 ))
  if [ "$col2" = 9030 ]
  then
    col2=$i
  fi
  echo "$col1 $col2 $remainder"
done < example > expected

Sometimes it makes sense to use a case-esac.
The $(( )) can contain a modification of the variable.
Code:
i=9029
while read -r col1 col2 remainder
do
  case $col2 in
  ( 9030 )
    col2=$(( i+=1 ))
  ;;
  esac
  echo "$col1 $col2 $remainder"
done < example > expected

BTW here, like in your final awk solution, the $i is not incremented if there is no 1930
(Comment for the experts: bash-2 and ksh88 need i+=1 rather than ++i)
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Replace consecutive occurrence of string in same line
ureddy
Hi All, I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output. Input: --------- 123.5|ABC|.|.|. 234.4|DEF|.|.|.|.|.| Output: --------- 123.5|ABC|||. 234.4|DEF||||| so basically "|.|" need to be replaced with...... AIX
9
AIX
Replace x Number of String Occurrence with Sed
SkySmart
Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use...... Shell Programming and Scripting
10
Shell Programming and Scripting
remove characters from string based on occurrence of a string
victor369
Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail...... Shell Programming and Scripting
5
Shell Programming and Scripting
SED replace string by occurrence
uttamhoode
hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3... Shell Programming and Scripting
4
Shell Programming and Scripting
Search and replace to first occurrence of string
gilmord
Hi all, I have a very large; delimited file. In vi I would like to replace: CSACT_DY;AVG_UEACT1;uesPerActiveLinkSetSize_1;#;A CSACT_DY;AVG_UEACT2;uesPerActiveLinkSetSize_2;#;A CSACT_DY;AVG_UEACT3;uesPerActiveLinkSetSize_3;#;A with: CSACT_DY;AVG_UEACT1;Average...... UNIX for Dummies Questions & Answers
7
UNIX for Dummies Questions & Answers

Featured Tech Videos