Sponsored Content
Top Forums Shell Programming and Scripting Problem getting sed to work with variables Post 303013910 by Chubler_XL on Thursday 1st of March 2018 07:46:21 PM
Old 03-01-2018
This solution as 1 awk program appears to work OK with your test data.

Note: I used index() and substr() instead of gsub() to avoid possible RE character issues that MadeInGermany identified.

Code:
# usage ./rename_duplicates.sh  test_base.txt  test_sdf.sdf

# base file to check for duplicate names
base_file=$1
# sdf with duplicate structures
sdf_file=$2

awk '
  BEGIN { FS=OFS="\t" }
  FNR==1{ if(file++ > 1) {printf "" > "revised_"FILENAME } }
# process the base_file and count the dups, +1 if a dup was met
  file==1 { if (dup[$2]++==1) dup[$2]++; next }

# process the base_file again, if a dup then add a dup_#_ prefix
  file==2 && dup[$2]>1 { repcnt[$2]++; $2=("dup_" repcnt[$2]-1 "_" $2)}
  file==2 { print >> "revised_"FILENAME }

# replace all duplicate keys in repcnt[] with the dup_ddd string
  file==3 {
      for(check in repcnt) {
          pos=index($0, check)
          if (pos) {
             fdup[check]++
             old=$0
             $0=""
             while(pos) {
                 $0=$0 substr(old,1,pos-1) "dup_" fdup[check] - 1 "_" check
                 old=substr(old, pos + length(check))
                 pos=index(old, check)
             }
             # Some efficiency - when all dups replaced dont check for it again
             if (fdup[check] == repcnt[check]) delete repcnt
             $0=$0 old
          }
      }
      print $0 "\n$$$$" >> "revised_"FILENAME
  }
 ' "$base_file" "$base_file" FS="" RS="\n[$]{4}\n" $sdf_file


Last edited by Chubler_XL; 03-01-2018 at 09:00 PM..
This User Gave Thanks to Chubler_XL For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Working with Script variables; seems like this should work...

The following seems quite basic but does not seem to work. Anybody know why? $ g=1 $ echo $g 1 $ echo abc$g abc1 $ abc$g=hello ksh: abc1=hello: not found $ echo $abc1 ksh: abc1: parameter not set It works when I specify the full variable name $ abc1=hello $ echo $abc1 hello ... (2 Replies)
Discussion started by: Chong Lee
2 Replies

2. UNIX for Dummies Questions & Answers

sed command not work with variables?

I am trying to write a simple script which will take a variable with sed to take a line out of a text and display it #!/bin/sh exec 3<list while read list<&3 do echo $list sed -n '$list p'<list2 done this does not work, yet when I replace the $list variable from the sed command and... (1 Reply)
Discussion started by: MaestroRage
1 Replies

3. Shell Programming and Scripting

cd command doesn't work through variables

Hi.... cd command is not working when dual string drive/volume name is passed to cd through variables....... For Ex.... y=/Volumes/Backup\ vipin/ cd $y the above command gives error....... anyone with a genuine solution ? (16 Replies)
Discussion started by: vipinchauhan222
16 Replies

4. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies

5. Shell Programming and Scripting

Sed with variables problem

I am writing a script with a sed call that needs to use a variable and still have quotations be present in the substitution. Example: sed -i "s/Replacable.\+$/Replaced="root@$VAR"/g" this outputs: where $VAR = place Replaced=root@place and i need Replaced="root@place" ... (2 Replies)
Discussion started by: mcdef
2 Replies

6. Shell Programming and Scripting

reading external variables does not work

... declare vINIFILE vINIFILE=$1 ... echo "The name of the File is $vINIFILE" >>mail_tmp echo "" >> mail_tmp.$$ ... grep RUNJOB=0 $vINIFILE >>tmp_filter ... So the strange is in echo-statement I get the correct output for $vINIFILE wrtitten into the file mail_tmp. But the... (2 Replies)
Discussion started by: ABE2202
2 Replies

7. Shell Programming and Scripting

Perl variables inside Net::Telnet::Cisco Module doesn't work

I am writing perl script to configure Cisco device but Variables inside Net::Telnet::Cisco Module doesn't work and passed to device without resolving. Please advise. here is a sample of script: use Net::Telnet::Cisco; $device = "10.14.199.1"; ($o1, $o2, $o3, $o4) = split(/\./,$device);... (5 Replies)
Discussion started by: ahmed_zaher
5 Replies

8. UNIX for Dummies Questions & Answers

Why does this SED example work?

$ x="/home/guru/temp/f1.txt" $ echo $x | sed 's^.*/^^' This will give the absolute path f1.txt. I don't understand WHY it works. How is it determining the last "/" character exactly? (7 Replies)
Discussion started by: glev2005
7 Replies

9. Shell Programming and Scripting

Problem with variables in sed

Hello! I have a problem to insert variables with sed... And I can't find the solution. :confused: I would like to display one/few line(s) between 2 values. This line works well sed -n '/Dec 12 10:42/,/Dec 12 10:47/p' Thoses lines with variables doesn't work and I don't find the... (2 Replies)
Discussion started by: Castelior
2 Replies

10. Shell Programming and Scripting

[Bash] passing variables to executable doesn't work

Bash version 4.4.20 / Ubuntu 16.0.4 Hello, I tried to write a script that gathers some data and passes them to an executable. The executed application answers with an error. The echo output in the script returns correct values. If I copy/paste the last echo command, it get's executed... (2 Replies)
Discussion started by: sushi2k7
2 Replies
TRANSITION-CHECK(1)													       TRANSITION-CHECK(1)

NAME
transition-check - check a package list for involvement in transitions SYNOPSIS
transition-check --help|--version transition-check [-f|--filename=FILENAME] [source package list] DESCRIPTION
transition-check checks whether any of the listed source packages are involved in a transition for which uploads to unstable are currently blocked. If neither a filename nor a list of packages is supplied, transition-check will use the source package name from debian/control. OPTIONS
-f, --filename=filename Read a source package name from filename, which should be a Debian package control file or .changes file, and add that package to the list of packages to check. EXIT STATUS
The exit status indicates whether any of the packages examined were found to be involved in a transition. 0 Either --help or --version was used, or none of the packages examined was involved in a transition. 1 At least one package examined is involved in a current transition. LICENSE
This code is copyright by Adam D. Barratt <adam@adam-barratt.org.uk>, all rights reserved. This program comes with ABSOLUTELY NO WARRANTY. You are free to redistribute this code under the terms of the GNU General Public License, version 2 or later. AUTHOR
Adam D. Barratt <adam@adam-barratt.org.uk> Debian Utilities 2013-12-23 TRANSITION-CHECK(1)
All times are GMT -4. The time now is 10:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy