Find and replace increament


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace increament
# 1  
Old 04-26-2018
Linux Find and replace increament

Dears,

Can you help me with the below concern, I need one liner for this.

Input_file.txt
Code:
dn: asdf=yyyyyyyyyyyyy426019001711473,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001711473
DSUnitGroup: 1
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001711351,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001711351
DSUnitGroup: 3
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001711436,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001711436
DSUnitGroup: 8
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001711326,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001711326
DSUnitGroup: 16
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001710223,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001710223
DSUnitGroup: 6
objectClass: CUDBMultiServiceConsumer

The output should be like below, the should start from DSUnitGroup: 1 and incremented sequentially till it reach a particular count here let us say DSUnitGroup: 8, and once it is reached again it should start from DSUnitGroup: 1.

Output_file.txt
Code:
dn: asdf=yyyyyyyyyyyyy426019001711473,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001711473
DSUnitGroup: 1
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001711351,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001711351
DSUnitGroup: 2
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001711436,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001711436
DSUnitGroup: 3
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001711326,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001711326
DSUnitGroup: 4
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001710223,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001710223
DSUnitGroup: 5
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001710223,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001710223
DSUnitGroup: 6
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001710223,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001710223
DSUnitGroup: 7
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001710223,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001710223
DSUnitGroup: 8
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001710223,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001710223
DSUnitGroup: 1
objectClass: CUDBMultiServiceConsumer

dn: asdf=yyyyyyyyyyyyy426019001710223,ou=multiSCs,dc=btc
asdf: yyyyyyyyyyyyy426019001710223
DSUnitGroup: 2
objectClass: CUDBMultiServiceConsumer

can you help me with the one liner string. to achieve this.

Last edited by rbatte1; 04-26-2018 at 02:23 PM.. Reason: Added CODE tags
# 2  
Old 04-26-2018
Code:
awk '$1 ~ /DSUnitGroup:/ {$NF=((c++ % count) + 1)} 1' count=8 file

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 04-26-2018
@rdrtx1 thank you for the reply brother, can you also help me in describing what this awk attributes are doing I'm not very sound in this.
# 4  
Old 04-27-2018
Like so (assuming you know about the pattern - action pair operation of awk):
Code:
awk '                                   # run awk and open its first parameter, taken as "awk script"
$1 ~ /DSUnitGroup:/                     # if $1 (the first field) matches "DSUnitGroup"
  {$NF=                                 # assign to $NF (the last field) the ---
        (
                (c++                    # --- contents of the (post-incremented, see below) c variable
                 % count                # modulo count variable's contents (8) i.e. 0 - 7
                ) + 1                   # plus 1, i.e. 1 - 8
        )                               # now increment c
  }                                     # end statement for $1 match
1                                       # for 1 = TRUE perform default action (print $0)
'                                       # close awk's first parameter
 count=8 file                           # specify count value and input file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace

Hi, I have a variable which holds the below value: echo $FROM_DIR /fsg/fgldevu/fs_ne/inst/FGLDEVU_01/logs/appl/conc/out/o14946708.out I want to change the value in the variable as below: out/o to be replaced with log/l .out to be replaced with .req The desired output should look like... (5 Replies)
Discussion started by: Prasannag87
5 Replies

2. Shell Programming and Scripting

Find and replace

Hi , i have a file with some data and i need replace a perticular value with some other value from another another file req: file1.txt abc,idle.txt,1234 file2.txt 5678 now the requirement is need to replace the "idle.txt" is with "mike_5678" here 5678 from file2.txt... file2.txt... (1 Reply)
Discussion started by: dssyadav
1 Replies

3. Shell Programming and Scripting

find and replace

Hi, I have to grep value from one file, if that value is already present with "#" symbol. I have to remove that symbol in that file. Ex: file1.txt contains the following string #value=15 I have to search for "Value". If I found the string with hash symbol, nee to remove the # in... (6 Replies)
Discussion started by: ckchelladurai
6 Replies

4. Shell Programming and Scripting

Using records iteratively with increament

I am very new to unix. Trying to use a for loop for this task I have a file like this abc.uml ccc.uml ddd.uml rrr.uml kkk.uml . . . n.uml ( more than 10000) I want to read this file but just 5 names at a time and then again it should read next 5 names and agin next 5. this... (6 Replies)
Discussion started by: nnani
6 Replies

5. Shell Programming and Scripting

VI - Find and replace

Hi All, How can i replace a pattern where its with the pattern / To be more clear, this is what am trying to replace with Current pattern => /abc/xyz Replace to => /xyz/abc Giving the commad :%s/"/abc/xyz"/"/xyz/abc"/g throw me off. :wall: Awaiting for your assistance... (3 Replies)
Discussion started by: kusathy
3 Replies

6. Shell Programming and Scripting

find and replace

Hi I am very new to shell scripting (and linux in general)... I am trying to build some script, that will read a person from a csv file, (each person will have an ID, eg id001abc, which will go up one by one a few hundred times) then when find and replace the matching data in an xml file. ... (4 Replies)
Discussion started by: sam.breslin
4 Replies

7. UNIX for Dummies Questions & Answers

find and replace

I have a tab-delimited inFile: cat inFile A B C D E F 1 2 3 4 5 6 a b c d e fI would like to replace the first 3 tabs in each row with underscore to get outFile: A_B_C_D E F 1_2_3_4 5 6 a_b_c_d e fhow can I modify the following... (5 Replies)
Discussion started by: jdhahbi
5 Replies

8. Solaris

find/replace?

Dear All To find a file, according to you, I tried as: #find / -name file-name -print To find a string inside the files , I tried as : #find / -name "*" |xargs grep "string" Can you please let me know how can I try for find/replace (i.e. finding the intended string inside the text files... (6 Replies)
Discussion started by: hadimotamedi
6 Replies

9. UNIX for Dummies Questions & Answers

Find and Replace

After running a command like grep -ir files2/ * This will find all the files that contain "files2/" in it. For example if it finds files2/dir/today files2/dir/yesterday files2/dir/2daysago Now it may find 100 instances, so is there a quick find and replace command I can use? I... (4 Replies)
Discussion started by: NycUnxer
4 Replies

10. Shell Programming and Scripting

find and replace

Hi, Iam new to shell script.How to write bourn shell script for find and replace. My requirement is: variable: name="abcd & co" i wanted to replace '&' with amp; plz suggest with out using sed. Thanks lot. (2 Replies)
Discussion started by: ram2s2001
2 Replies
Login or Register to Ask a Question