LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-22-2009, 08:45 PM   #1
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Rep: Reputation: 31
Need some help cutting a file up


I have a file that has one line in it, and looks like this:
Code:
-a test.example.com -a next.example.com -a last.example.com
I'm trying to code a bash script that will cut the file up, and only give me the *.example.com portions. I imagine I'll need the cut command (or multiple cut commands). Something like this:

Code:
list=`cat thefile | cut -d"-"
I know that's not a working example, but I wanted to see if anyone else had attempted something similar before and could give me an example. The number of entries can be variable and the domain names can of course be anything, but it will always be in the same format and on one line.
Thanks in advance!
 
Old 10-22-2009, 09:00 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
say for example
Code:
$ a="-a test.example.com -a next.example.com -a last.example.com"
$ set -- $(echo ${a//-a/})
$ echo $@
test.example.com next.example.com last.example.com
$ echo $1
test.example.com
$ echo $2
next.example.com
$ echo $3
last.example.com
in a file
Code:
while read -r line
do
    set -- ${line//-a/}
    echo $1 
    echo $2
    echo $3
done < "file"

Last edited by ghostdog74; 10-22-2009 at 09:02 PM.
 
Old 10-22-2009, 09:03 PM   #3
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Original Poster
Rep: Reputation: 31
Hmm. That gets me running in the right direction I think. I do not know the number of entries in the file, but I can play and get that from "echo $@" I think.
Thanks!
 
Old 10-23-2009, 12:06 AM   #4
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
I would use 'sed' to find all the lines containing the "-a" parameters then substitute the "-a" with nothing

Code:
sed -n -e 's/-a//p' file.txt
that's 'sed' '-n' (print only addressed lines specified with p) '-e'(xpression) 's'(ubstitute) '-a' with '' p(rint) for file.txt


there is also a way to use two expressions to first find a particular type of line (maybe it always starts with the same command) then process only those lines with the substitution you want.

Last edited by lumak; 10-23-2009 at 12:08 AM.
 
Old 10-23-2009, 12:16 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by lumak View Post
I would use 'sed' to find all the lines containing the "-a" parameters then substitute the "-a" with nothing
Code:
sed -n -e 's/-a//p' file.txt
you forgot the "g" modifier. also, in OP's case, if he wants to use each example.com individually, then sed is not the correct tool for this purpose. A more "flexible" approach is to bring each example.com into an array of some sort so that each example.com may be used individually. Eg maybe OP wants to ping each host etc etc.
 
Old 10-23-2009, 02:06 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Perl's split() fn http://perldoc.perl.org/functions/split.html does the job in one
Code:
# split on string, not just single char
$var1 = "-a test.example.com -a next.example.com -a last.example.com";
@arr = split(/-a /, $var1);
print "@arr\n";

test.example.com  next.example.com  last.example.com
 
Old 10-23-2009, 03:12 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
so is awk
Code:
awk 'BEGIN{FS="-a "}{print $1,$2,$3,$4}'  file
 
Old 10-24-2009, 10:50 AM   #8
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
@ghostdog74, as you have it, awk would need some kind of internal loop, not to mention it would print a blank line as the first option. AND print every line, as is, before and after that. Also... the ideal solution would be to find the most generalized script that will work on any file with a '-a' formatted line. At the moment, we don't even know if the line starts with a command or if it follows a line that has a '\'.

Last edited by lumak; 10-24-2009 at 10:52 AM.
 
Old 10-24-2009, 11:39 AM   #9
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
How about this
Code:
#!/bin/bash

while read line
do
    array=($line)
    for ((i=1; i<${#array[*]}; i=i+2))
    do
        name="${array[$i]}"
        suffix="${name#*.}"
        echo "DEBUG: suffix is '$suffix'"
    done
done < input.txt
 
Old 10-24-2009, 07:01 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by lumak View Post
@ghostdog74, as you have it, awk would need some kind of internal loop, not to mention it would print a blank line as the first option.
that's not a problem at all. Can be easily fixed. The crux is, whether OP wants to use each domain individually. If he wants to use each domain found individually, he can always grab them from the arrays/fields. Doing only a substitution serves nothing, except if OP only wants to make output nice.
 
Old 10-24-2009, 08:40 PM   #11
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
my point was that both of our solutions were incomplete and further education on the OP's end would be required to make it usable. That and sending my first solution (yes yes with the 'g' option) into a variable would allow further processing later in the script anyway. the -n prints only the lines with -a in it then it substitutes -a with nothing. Web addresses don't have spaces or return characters. The variable storing the output of sed can then be processed anyway imaginable. bash will treat both the return character and a space as white space. The only flaw in my solution is if there is something before and after on the same line with the '-a' but then you could do other tests to figure that out. So no. substituting doesn't only make the output nice. It's part of the process of an incomplete solution.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cutting the last N characters form a file name Rindert Linux - General 20 08-18-2006 06:38 AM
MPlayer cutting off the start of each file nutthick Linux - Software 3 04-26-2006 12:40 PM
Sed Help Needed..Cutting out peice of file farmerjoe Programming 2 02-25-2005 06:05 PM
Cutting in Bash??? mattyk1026 Programming 4 06-09-2004 01:23 PM
cutting down contents of text file kam_kenneth Linux - Newbie 2 05-22-2004 01:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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