LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2015, 02:27 AM   #1
Leanux
LQ Newbie
 
Registered: Jan 2015
Posts: 4

Rep: Reputation: Disabled
Inquiry on a bash script using sed and grep -c


Hi Everyone,

I need some help on my bash script, I'm trying to rename a certain line in a file which might be 1 or more.

IDXCOUNT=`grep -c 'index .* on ' $FILENAME`;

for n in $(eval echo {1..$IDXCOUNT});
do
timestamp=$(date +"%s");
echo "Renaming index idx_$timestamp..";
if [ $n -eq 1 ]; then
sed -i "0,/^index [^)]* on /s/index idx_$timestamp on /" $FILENAME;

My problem is, if the sed target is 2 or more it will generate an idx_$timestamp that will cause duplicate index value when the script finish. My goal is to have the script recognize that when there are multiple index in the file, it will try to rename it one by one.

I'm new to bash so I'm not sure if I explained my issue well but I will appreciate any help!!

Thanks!

Last edited by Leanux; 01-20-2015 at 02:29 AM.
 
Old 01-20-2015, 10:10 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian Sid. Arch. In the process of converting to and learning FreeBSD
Posts: 1,023
Blog Entries: 6

Rep: Reputation: 237Reputation: 237Reputation: 237
You mean in more than one place, the time stamp is identical? You're using a time stamp in seconds, and if it processes more than one such line in less than a second, you're going to have as many identical lines as the script can do within a second.
 
Old 01-20-2015, 10:48 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Quote:
My problem is
Your first problem is thinking you have only one ... I would suggest restating what it is you are trying to do, as the current script will only lead you to more bad scripting habits.

Please also use [code][/code] tags around code and data to make them more legible
 
Old 01-21-2015, 04:41 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
grail has some good points.
I'd simplify the first line
Code:
for n in $(seq IDXCOUNT)
You should also consider using 'set -vx' at the top of your program for debugging
 
Old 01-21-2015, 08:20 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Ok ... you went from asking a question to SOLVED. Good to see you worked it out, would you like to share so others may learn from you?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash- sed, awk, grep schneidz Programming 10 01-31-2011 09:56 PM
working on bash: wante to no what a combinasion of commands(grep, sed) do ? coolloo_djack Programming 4 03-12-2010 04:24 AM
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 10:30 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 07:30 AM


All times are GMT -5. The time now is 05:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration