Advertisement

05.04.2004 at 07:57AM PDT, ID: 20977349
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Bash script to parse line by line

Tags: bash, line, file, read, script
I have a file with the following format:
d    28854           cpmnt    open     username  owner  3   062 description of problem
d    28855           cpmnt    open     username  owner  3   062 description of problem
d    28859_d           cpmnt    open     username  owner  3   062 description of problem


I need to "grab" the second token of every line, no matter how large the token is. The 2nd token is characterized by whitespace delimerters on both sides. (generally spaces, but I would like to be able to handle tabs as well).
I would like generate a new file and insert onto each line the 2nd token from this first file.
This is a fun programming excercise. I have been working on it for a little while now, but just do not know the commands wlel enough to be able to get it where i need it to be.

The faster the response on this, the more points I will add to the question, and the better grade I will offer. Please help!

Here is the relevant portion of the script that I have so far (not a big deal at all)
It simply reads in the file line by line and outputs it to the terminal

# START OF SCRIPT
exec < currentDefectsLog
while read line
do
echo $line
done
#END OF SCRIPT
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: jameswalt
Solution Provided By: PaulS_III
Participating Experts: 2
Solution Grade: A
Views: 842
Translate:
Loading Advertisement...
05.04.2004 at 08:51AM PDT, ID: 10987933

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.04.2004 at 09:04AM PDT, ID: 10988065

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.04.2004 at 09:05AM PDT, ID: 10988077

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.04.2004 at 09:13AM PDT, ID: 10988154

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.04.2004 at 10:15AM PDT, ID: 10988837

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.04.2004 at 10:22AM PDT, ID: 10988904

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.04.2004 at 10:25AM PDT, ID: 10988936

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.04.2004 at 11:52AM PDT, ID: 10989699

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.05.2004 at 04:44AM PDT, ID: 10994998

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.04.2004 at 08:51AM PDT, ID: 10987933
try:

echo $line | awk '{ print $2 }'

That should work for ya


Paul
 
05.04.2004 at 09:04AM PDT, ID: 10988065
That works nicely!

Any idea how to make it so that it starts reading/printing on the third line?
 
05.04.2004 at 09:05AM PDT, ID: 10988077
also, have it so that it stops on the 2nd to last line?

Ill up the points if you can help me with these.
 
05.04.2004 at 09:13AM PDT, ID: 10988154
Don't know that this is the best way but you could set an accumulator and increment it

x=1

exec < currentDefectsLog
while read line
do

if [ $x -gr 1 ]
then
echo $line | awk '{ print $2 }'
fi

x=(($x + 1))
done

that will get you to start at line 2

Then to stop at the second line from the end we can do this

filelinecount=` wc - l | cat currentDefectsLog`

then inside the while we can do this

exec < currentDefectsLog
while read line
do

if [ $x -gr 1 ]
then
echo $line | awk '{ print $2 }'
fi

if [ $x -eq (($filelinecount - 2)) ]
then
exit 0
fi

x=(($x + 1))

done

put it all together and I think that will take care of it.

Let me know if you require additional help.

Paul
 
05.04.2004 at 10:15AM PDT, ID: 10988837
i have not tried those out yet.. but how do I say:

"if a directory does NOT exist then do whatever" ?

ive tried

if [ ! -d directory]
then
  run some code
fi


but that doesnt work.
 
05.04.2004 at 10:22AM PDT, ID: 10988904
That is the way I check my directories.  

One thing I have learned though is there must be spaces between the braces and the test cirteria:
[ ! -d directory ]

If there are no spaces, things tend not to work.

Oh and another thing, in the code I gave you above you might need to change it a bit. I think the x=(($x + 1)) should probably be x=$(x + 1). Try it both ways.

Paul
Accepted Solution
 
05.04.2004 at 10:25AM PDT, ID: 10988936
n/m i got it
 
05.04.2004 at 11:52AM PDT, ID: 10989699
Thank you for your continuous help. I will add more points as soon as soon as a refund comes in from an unanswered question.

How would I make the script do the following:

read a file line by line where each line is a single token of alphanumeric characters, and "_" characters
  **trim white spaces from line**
  if line is NOT blank then
    if directory /defects/$line exists then
      delete /defects/$line
    fi
  fi
done

I tried doing this:

#---start of script

while read line
do
        line= echo $line | awk '{print $0}'
        if [ -n $line ]
        then
                if [ -d "/defects/$line" ]
                then
                        rm /defects/$line -f -r
                fi
        fi
done

#--- end of script

it almost works - except one thing. if it encounters a line with just whitespace on it, it will delete the /defects directory - when I never want that deleted as it is. i want to skip all blank lines.
 
05.05.2004 at 04:44AM PDT, ID: 10994998
ill have a shorter one for you

#!/bin/bash
#start
#Usage ./scriptname <file>

#Gives you the total number of lines in the file
nrow=`wc -l $1 | awk '{print $1}'`
#Nr or rows to tail the file with all rows - 3
rowtail=$(($nrow - 3))
#Nr or rows to head the file with all rows - 2
rowhead=$(($rowtail - 2))

#Output the right fields
tail -n $rowtail $1 | head -n $rowhead | awk '{ print $2 }'

#end

/Rob
 
 
20080236-EE-VQP-29