ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
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?
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?
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
Rep:
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
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
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.