LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2009, 06:00 PM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
need help with a bash script


first thing this is not homework
ok i got a file laid out like this
[code]
00:00:01.871 <bla> <bla> <bla>
00:00:02.665 <bla> <bla> <bla>
09:15:34.003 <bla> <bla> <bla>
[code]
i want to set the forth number of evrey line to zero
all in a bash script
is it possable?
 
Old 10-06-2009, 06:32 PM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by smeezekitty View Post
first thing this is not homework
ok i got a file laid out like this
[code]
00:00:01.871 <bla> <bla> <bla>
00:00:02.665 <bla> <bla> <bla>
09:15:34.003 <bla> <bla> <bla>
[code]
i want to set the forth number of evrey line to zero
all in a bash script
is it possable?
The what number? Give a specific example:

I have this as an input ___________________

I want this as an output __________________

Leave nothing to the imagination.
 
Old 10-06-2009, 06:36 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
input 00:00:01.555
output 00:00:01.000
--or--
input 01:32:15.003
output 01:32:15.000
 
Old 10-06-2009, 08:07 PM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by smeezekitty View Post
input 00:00:01.555
output 00:00:01.000
--or--
input 01:32:15.003
output 01:32:15.000
Code:
$ x="00:00:01.555"
$ echo ${x%%.*}.000
00:00:01.000
I have a tutorial on my Website that covers this sort of thing:

Bash Shell Programming in Linux
 
Old 10-06-2009, 08:14 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
not quite right
here is some example text from the file
Code:
00:00:01.879   Trusted       <integer> = 0x0000000000000001 (1)
00:00:01.879   PCIDeviceNo   <integer> = 0x0000000000000001 (1)
00:00:01.879   PCIFunctionNo <integer> = 0x0000000000000001 (1)
00:00:01.879 
00:00:01.879 [/Devices/piix3ide/0/Config/] (level 4)
00:00:01.880   Type <string>  = "PIIX4" (cch=6)
         ^^^
         \|/
          V
          |_this needs to be changed to zero on every line
 
Old 10-07-2009, 01:51 AM   #6
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by smeezekitty View Post
not quite right
here is some example text from the file
Code:
00:00:01.879   Trusted       <integer> = 0x0000000000000001 (1)
00:00:01.879   PCIDeviceNo   <integer> = 0x0000000000000001 (1)
00:00:01.879   PCIFunctionNo <integer> = 0x0000000000000001 (1)
00:00:01.879 
00:00:01.879 [/Devices/piix3ide/0/Config/] (level 4)
00:00:01.880   Type <string>  = "PIIX4" (cch=6)
         ^^^
         \|/
          V
          |_this needs to be changed to zero on every line
lustup gave you the command based on the input from your previous
message. Now you can use the same idea with the full line
Say your input file is input.txt and you want to output to output.txt
then you could

Code:
rm -f output.txt
cat input.txt | while read line
do
    x=$(echo $line | awk '{print $1}'
    y=${x%%.*}.000
    #new line is eg: '00:00:01.000'
    #followed by '   Trusted       <integer> = 0x0000000000000001 (1)'
    newline=${y}"$(echo $line | sed 's?^[^ \t]*??')"
    echo "$newline" >> output.txt
done
 
Old 10-07-2009, 02:23 AM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by smeezekitty View Post
not quite right
here is some example text from the file
Code:
00:00:01.879   Trusted       <integer> = 0x0000000000000001 (1)
00:00:01.879   PCIDeviceNo   <integer> = 0x0000000000000001 (1)
00:00:01.879   PCIFunctionNo <integer> = 0x0000000000000001 (1)
00:00:01.879 
00:00:01.879 [/Devices/piix3ide/0/Config/] (level 4)
00:00:01.880   Type <string>  = "PIIX4" (cch=6)
         ^^^
         \|/
          V
          |_this needs to be changed to zero on every line
Quote:
not quite right
Not explained quite right. Try this:

Code:
while read line
do
    echo ${line/.[0-9][0-9][0-9]/.000}
done
Result:

Code:
00:00:01.000   Trusted       <integer> = 0x0000000000000001 (1)
00:00:01.000   PCIDeviceNo   <integer> = 0x0000000000000001 (1)
00:00:01.000   PCIFunctionNo <integer> = 0x0000000000000001 (1)
00:00:01.000
00:00:01.000 [/Devices/piix3ide/0/Config/] (level 4)
00:00:01.000   Type <string>  = "PIIX4" (cch=6)
 
Old 10-07-2009, 10:29 AM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
lutusp, I wne to look at your tutorial right away, but I was a little disappointed...
You spent many lines teaching things which are not bas at all:
date
whoami
ls
find
tree
file
cat
more
grep
wc
But, the worst mistake is confusing /bin/test with the bash builtin test and several other builtin commands:
'man test' wil give you the man-page for /usr/bin/test
But, if you use simply 'test' in the shell, you'll be using bash's builtin test which is not the same. Same goes for cd, pwd and [ (or [[.

Use 'type test' to discover whether your 'test' is a builtin. If you use 'which test' you'll get /usr/bin/test, but 'type test' will tell you that test is a bash builtin. So, the real way to get help for the 'test' command is to use 'help test' and the same for the others.
 
Old 10-07-2009, 12:02 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
@vonbiber
Code:
bash-2.04$ rmv.sh
./rmv.sh: line 4: unexpected EOF while looking for matching `)'
./rmv.sh: line 11: syntax error: unexpected end of file
bash-2.04$
 
Old 10-07-2009, 12:34 PM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Alternative using sed:
Code:
sed -i 's/\.[0-9][0-9][0-9]/\.000/' file
Make sure you have a backup of the original file while testing that it doesn't have any unforseen issues.
 
Old 10-07-2009, 12:52 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by smeezekitty View Post
@vonbiber
Code:
bash-2.04$ rmv.sh
./rmv.sh: line 4: unexpected EOF while looking for matching `)'
./rmv.sh: line 11: syntax error: unexpected end of file
bash-2.04$
Bash said the problem was on line 4; a closing ) is needed after $line.
 
Old 10-07-2009, 01:11 PM   #12
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
./rmv.sh: cat: Too many open files (EMFILE)
oh great
 
Old 10-07-2009, 02:56 PM   #13
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
smeezekitty, I notice you are using bash-2.04. That may have some bearing on any problems as it lacks certain features available in later versions. If you really need to use bash-2.04, you might want to put #!/bin/sh as the shebang instead of #!/bin/bash and then just try to make it all POSIX-compatible instead of using bashisms.
 
Old 10-08-2009, 01:33 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
assuming index starts with 0:

Code:
#!/bin/bash

IF=$1 OF=$2

{
	while read LINE; do
		echo "${LINE:0:9}0${LINE:12}"
	done
} < "$IF" > "$OF"
or

Code:
#!/bin/bash

IF=$1 OF=$2

shopt -s extglob

{
	while read LINE; do
		A=${LINE:0:9}
		B=${LINE:9}
		B=${B##+([[:digit:]])}
		echo "${A}0${B}"
	done
} < "$IF" > "$OF"
sometimes, {} is no longer needed but it's more consistent, i think

Last edited by konsolebox; 10-08-2009 at 01:36 AM.
 
Old 10-08-2009, 03:24 AM   #15
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by gnashley View Post
lutusp, I wne to look at your tutorial right away, but I was a little disappointed...
You spent many lines teaching things which are not bas at all:
The title of the article is "Bash Shell Programming in Linux". Nowhere does it say or require that all the discussed commands are built-ins. That isn't the point. The point is for newbies to learn how to get results using a Bash shell session, not to limit themselves to Bash built-ins.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 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