LinuxQuestions.org
Review your favorite Linux distribution.
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 06-07-2013, 01:53 PM   #1
emilioveras
LQ Newbie
 
Registered: Jun 2013
Posts: 1

Rep: Reputation: Disabled
Post Command Interpretation


We are running this script:


Code:
if [ $# = 1 ] then
   if [ -f $ ] then 
      sed '1,$ s/\(^........\)\(.*\)/\1/g' < $ > tmp
      mv $ $.bak
      mv tmp $
   else
      echo  
      echo "File doesn’t exists"
       echo
   fi
else
   echo
   echo "Format is correct!"
fi
We do not understand what this line of code is doing:

Code:
sed '1,$ s/\(^........\)\(.*\)/\1/g' < $1 > tmp
Any help will be appreciated.

Regards;

Emilio.-
 
Old 06-07-2013, 03:31 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
sed '1,$ s/\(^........\)\(.*\)/\1/g' < $1 > tmp
This looks for the first 7 characters (back-referenced) followed by any character (also back-referenced), which is replaced by the first 7 characters. It does this for line 1 to end and globally on the line.

That sed statement is very poorly written!!

This would be better (there are probably other ways as well):
Code:
sed 's/\(^........\).*/\1/' < $1 > tmp
Also this would not be needed when using a modern sed (4+) version:
Code:
sed '1,$ s/\(^........\)\(.*\)/\1/g' < $1 > tmp
mv $1 $1.bak
mv tmp $1
All that can be replaced by:
Code:
sed -i.bak 's/\(^........\).*/\1/' < $1
 
1 members found this post helpful.
Old 06-09-2013, 05:28 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Even better, use the -r option and you can eliminate the backslashes, as well as use the {n} regex repeating pattern (assuming this is gnu sed and not one of the more limited unix versions).

And the "<" redirection isn't needed, since sed can read directly from files.

Code:
sed -r -i.bak 's/(^.{8}).*/\1/' "$1"
Or perhaps better still, use cut instead.

Code:
cp "$1" "$1.bak"
cut -c 1-8 "$1.bak" >"$1"

But more important than that...

QUOTE ALL OF YOUR VARIABLE EXPANSIONS! You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and any possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions when you need them.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Also, when using advanced shells like bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

(Is anyone else seeing this page without css formatting? There seems to be a database error of some kind.)

Last edited by David the H.; 06-09-2013 at 05:34 AM. Reason: bit of re-coding
 
1 members found this post helpful.
Old 06-09-2013, 08:58 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
RE css: saw a page the other day sans css; might have been this one...
 
Old 06-11-2013, 11:36 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Yeah, I found the feedback thread for it after I posted. It's all cleaned up now (obviously).

http://www.linuxquestions.org/questi...0/#post4968250
 
  


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] Change to Find command interpretation ilesterg AIX 10 04-24-2013 11:10 PM
[SOLVED] Find command interpretation ilesterg AIX 11 04-21-2013 11:34 PM
/etc/group interpretation mitusf Slackware 13 10-18-2012 05:53 AM
[SOLVED] ls output interpretation JJJCR Linux - Newbie 4 10-10-2011 02:17 AM
Command interpretation issue in Sed script file angel115 Programming 9 04-21-2006 07:26 PM

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

All times are GMT -5. The time now is 10:57 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration