script to eliminate left and right fields and to get the ouput.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to eliminate left and right fields and to get the ouput.
# 1  
Old 10-21-2009
Java script to eliminate left and right fields and to get the ouput.

Hi Experts,
I have a file as given below and want to filter out the filenames in it , by deleting left and right filds and to have the fllenames (There are spaces in the filename),

Code:
Sun Jan 11 11:20:10 2009 1 0 /home/output/file2311_recent.list user1 user2 0 done
Sun Jan 11 11:20:10 2009 1 0 /home/output/file2312 jan recent.list user1 user2 0 done
Sun Jan 11 11:20:10 2009 1 0 /home/output/Output2313 feb recent.text user1 user2 0 done


I want to eliminate first 7 field from left side , and 4 fields from right side , and to get the output . Is there anything available with scripting , sed awk ?



Output should be:

Code:
/home/output/file2311_recent.list
/home/output/file2312 jan recent.list 
/home/output/Output2313 feb recent.text

Thanks in advance,

Last edited by vgersh99; 10-21-2009 at 07:28 PM.. Reason: code tags, please.
# 2  
Old 10-21-2009
Code:
nawk '{gsub("^[^/]*", "");$(NF-3)=$(NF-2)=$(NF-1)=$NF="";gsub(" *$", "")}1' myFile



---------- Post updated at 06:27 PM ---------- Previous update was at 06:25 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 10-21-2009
Code:
rev infile8|cut -d' ' -f5-|rev|cut -d' ' -f8-

Smilie

If all the file names contain a . with an extension then this would do too:
Code:
grep -o '/[^.]*\.[^ ]*' infile

# 4  
Old 10-21-2009
Many Thanks for the answers:

Scrutinizer , the rev and grep code worked nicely, However couldnot understand the grep code properly, appreciate if you could explain the grep code a bit.

vgersh99,
The nawk code looks nice but I could not use it, as nwak not available in redhat linux.
# 5  
Old 10-21-2009
if you have Python
Code:
# python -c "for line in open('file'): print ' '.join(line.split()[7:-4])"
/home/output/file2311_recent.list
/home/output/file2312 jan recent.list
/home/output/Output2313 feb recent.text

# 6  
Old 10-22-2009
Sure, rveri
Code:
grep -o '/[^.]*\.[^ ]*' infile

means look for a match that consists of
Code:
/      the character / ,
[^.]*  followed by any number of characters that are not a dot 
\.     followed by a dot
[^ ]*  followed by any number of characters that are not a space

and then from man grep:
Code:
-o     Print  only  the  matched  (non-empty)  parts  of  a  matching line, 
       with each such part on a separate output line.

Actually it is good you asked because now I realize that \. is not necessary, so the expression becomes:
Code:
grep -o '/[^.]*[^ ]*' infile


Last edited by Scrutinizer; 10-22-2009 at 01:06 AM..
# 7  
Old 10-22-2009
although OP's sample doesn't have other fields (including if filenames contains dots as well) containing dots in them, its prudent to include checks for them as well if using the above grep command. otherwise, eliminating by column is a more flexible way to go
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script ouput conversion

Hi All, I am trying to print all the packages info in solaris 11 using below script. #!/usr/bin/env bash pkginfo -l | egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' > /tmp/cp1 /usr/bin/nawk -F: ' {for (i=1; i<=NF; i++) {gsub (/^ *| *$/, "", $i) ... (5 Replies)
Discussion started by: sravani25
5 Replies

2. Shell Programming and Scripting

Shell Script has different ouput via cron vs when run Manually

Hello Lads, I deployed a script on my mac to start and stop EC2 instances on AWS console. The script when started manually on the terminal does the expected stop and start. Problem is when i try to schedule it on a cron, it fails to recognize the AWS Keys which i set up as ENV variable by... (2 Replies)
Discussion started by: Irishboy24
2 Replies

3. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies

4. Shell Programming and Scripting

Format Ouput

I have this input file Switch 0; Sun Sep 11 12:40:53 2011 EDT (GMT+4:00) 12:40:53.159984 SCN Port Offline;g=0x1e4b6 A2,P0 A2,P0 379 NA 12:40:53.159991 *Removing all nodes from port A2,P0 A2,P0 379 NA 18:45:31.326604 Port Elp engaged ... (1 Reply)
Discussion started by: greycells
1 Replies

5. Shell Programming and Scripting

Script to eliminate files .rlogin

Hi guys, I'm try making to script for eliminate files rlogins. path1='/home/*' for i in `cat /etc/passwd |awk -F: '{print $6}'`; do if test "$i" = "$path1"; then echo $i cd $i if ; then echo "$i/.rhosts detectado"|mail -s "rhosts" root ... (14 Replies)
Discussion started by: nena_redbalon
14 Replies

6. Shell Programming and Scripting

Limit ouput file on a shell script

I have this one::) doing jstack on JVM proccess. #!/bin/ksh # ---------------------------------------------------- # capture_jstack.sh <INTERVAL> <COUNT> # run jstack and capture output to a log file #----------------------------------------------------- # # scripts and logs are stored... (3 Replies)
Discussion started by: pointer
3 Replies

7. Shell Programming and Scripting

Shell script help to eliminate files of todays date

Hi I am very new to shell scripting and have written a script (below). However the directory I am searching will contain a file with a .trn extension each day which I want to eliminate. Each day the file extension overnight will change to trx, if this fails I want to know. Basically what I... (2 Replies)
Discussion started by: richM
2 Replies

8. UNIX for Dummies Questions & Answers

Combine fields and eliminate white space

Good Morning, Newbie here. Could someone help with shell scripting that will enable me to combine 2 fields into one eliminating the white space. The fields are fixed but the data of course varies. For example: First Name: "George " 20 positions" Last Name: "Washington " 30 positions" I need... (2 Replies)
Discussion started by: ski
2 Replies

9. Shell Programming and Scripting

Eliminate variable checking in a script

RH Linux, $SHELL=/bin/ksh I have a .profile which I source in as such --> . .profile Whats happening is the variables are getting validated and generating errors. for example .profile export foo=/to/the/moon when I . .profile , I get : not foundmyusername/.profile or bad... (8 Replies)
Discussion started by: BMetelsky
8 Replies

10. Shell Programming and Scripting

capture the ouput!

Hi, my perl script is calling another external java program. The Java in turn prints out a string. how can I capture the string. ------------------ #!/usr/bin/perl print "Content-type:text/html\n\n"; use CGI; $query = new CGI; $theCookie = $query->cookie('someCookie'); $user =... (0 Replies)
Discussion started by: azmathshaikh
0 Replies
Login or Register to Ask a Question