LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-08-2009, 04:24 AM   #16
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244

Quote:
Originally Posted by vonbiber View Post
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
no need cat.
 
Old 10-08-2009, 04:26 AM   #17
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by lutusp View Post
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.
you might want to change the title to "Shell programming in Linux" instead.
 
Old 10-08-2009, 04:29 AM   #18
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
if you are sure you want to change only the last 3 digits of the first column, then use awk
Code:
# awk '{gsub(/\.???$/,".000",$1)}1' file
00:00:01.879.000 Trusted <integer> = 0x0000000000000001 (1)
00:00:01.879.000 PCIDeviceNo <integer> = 0x0000000000000001 (1)
00:00:01.879.000 PCIFunctionNo <integer> = 0x0000000000000001 (1)
00:00:01.879.000
00:00:01.879.000 [/Devices/piix3ide/0/Config/] (level 4)
00:00:01.880.000 Type <string> = "PIIX4" (cch=6)
 
Old 10-08-2009, 12:38 PM   #19
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
Quote:
Originally Posted by ghostdog74 View Post
if you are sure you want to change only the last 3 digits of the first column, then use awk
Code:
# awk '{gsub(/\.???$/,".000",$1)}1' file
00:00:01.879.000 Trusted <integer> = 0x0000000000000001 (1)
00:00:01.879.000 PCIDeviceNo <integer> = 0x0000000000000001 (1)
00:00:01.879.000 PCIFunctionNo <integer> = 0x0000000000000001 (1)
00:00:01.879.000
00:00:01.879.000 [/Devices/piix3ide/0/Config/] (level 4)
00:00:01.880.000 Type <string> = "PIIX4" (cch=6)
bash-2.04$ awk
bash: awk: command not found
 
Old 10-08-2009, 03:54 PM   #20
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by ghostdog74 View Post
you might want to change the title to "Shell programming in Linux" instead.
I assure you, if I do that, someone will surely complain that the article is far too reliant on Bash and not enough on csh, ksh, zsh, Perl, Ruby, Python etc., etc. and that I am maliciously misleading my readers.

Believe me, no matter what you write or how you write it, someone will complain about it, usually someone who doesn't write articles himself.
 
Old 10-08-2009, 07:25 PM   #21
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by smeezekitty View Post
bash-2.04$ awk
bash: awk: command not found

let me guess. you are using bash 2.04 on windows...right?
 
Old 10-08-2009, 08:10 PM   #22
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
why you think?
 
Old 10-09-2009, 02:45 AM   #23
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
lutusp, please accept my apology for being so critical. I was having a fit of orneriness...

My guess is that you are doing this on DSL or on 'My own OS' whihc don't have awk.

Here, this works using sh:
Code:
#!/bin/sh

while read LINE ; do
	if [ "${LINE#* }" != "" ] ; then
		echo ${LINE%.*}.000 ${LINE#* }
	else
		echo ${LINE%.*}.000
	fi
done < data.txt
 
Old 10-09-2009, 02:59 AM   #24
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
Quote:
Originally Posted by gnashley View Post
lutusp, please accept my apology for being so critical. I was having a fit of orneriness...

My guess is that you are doing this on DSL or on 'My own OS' whihc don't have awk.

Here, this works using sh:
Code:
#!/bin/sh

while read LINE ; do
	if [ "${LINE#* }" != "" ] ; then
		echo ${LINE%.*}.000 ${LINE#* }
	else
		echo ${LINE%.*}.000
	fi
done < data.txt
stark verzerrt
 
Old 10-09-2009, 04:21 AM   #25
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by gnashley View Post
lutusp, please accept my apology for being so critical. I was having a fit of orneriness...

My guess is that you are doing this on DSL or on 'My own OS' whihc don't have awk.

Here, this works using sh:
Code:
#!/bin/sh

while read LINE ; do
	if [ "${LINE#* }" != "" ] ; then
		echo ${LINE%.*}.000 ${LINE#* }
	else
		echo ${LINE%.*}.000
	fi
done < data.txt
Umm, I already solved this in a more efficient way earlier in this thread:

Code:
while read line
do
    echo ${line/.[0-9][0-9][0-9]/.000}
done
Granted, this thread is getting long, but still.
 
Old 10-09-2009, 10:09 AM   #26
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by gnashley View Post
Here, this works using sh:
Code:
#!/bin/sh

while read LINE ; do
	if [ "${LINE#* }" != "" ] ; then
		echo ${LINE%.*}.000 ${LINE#* }
	else
		echo ${LINE%.*}.000
	fi
done < data.txt
your sh is linked to bash, correct? because those aren't sh (bourne) syntax.
 
Old 10-09-2009, 10:49 AM   #27
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by lutusp View Post
Umm, I already solved this in a more efficient way earlier in this thread:

Code:
while read line
do
    echo ${line/.[0-9][0-9][0-9]/.000}
done
Sorry, i didn't read this code properly. The code's not only simpler to the referred code but mine as well.
 
  


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