LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 01: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 01:29 AM.
 
Old 01-20-2015, 09:10 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
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, 09:48 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 03:41 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 07:20 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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



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 08:56 PM
working on bash: wante to no what a combinasion of commands(grep, sed) do ? coolloo_djack Programming 4 03-12-2010 03:24 AM
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 09:30 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 06:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:45 PM.

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
Open Source Consulting | Domain Registration