Grep words with spaces and save the output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep words with spaces and save the output
# 1  
Old 02-08-2013
Grep words with spaces and save the output

I have a file that contains the schedule for a tournament with 41 teams. The team names have spaces in them. I would like to search for each teams schedule and then save that to that teams file

For example

Team name: "Team Two"

I would like to search for all the games for "Team Two" and then the result I would like to save it to a file called Team_Two

All the teams names are in a file called Teams and few of the team names have spaces in them

Code:
TeamOne
Team Two
Team Three
TeamFour
Team Five O
Team Six
Team Seven

and so on ..

Team Forty one.


Code:
$ for i in `cat teams`
do
grep -i "$i" schedule >> $i (how do I replace the space before saving it)
done

The output I would like to expect is Each teams schedule saved in their team name

This is not really urgent but can someone please help me out

Last edited by Scrutinizer; 02-08-2013 at 05:37 PM.. Reason: code tags
# 2  
Old 02-08-2013
Code:
while read i
do
i2=$( echo "$i" | tr ' ' '_' )
grep -i "$i" schedule >> "$i2"
done < teams


Last edited by DGPickett; 02-11-2013 at 10:25 AM.. Reason: Change spaces in file name to _
# 3  
Old 02-11-2013
this still saves the team names with spaces.
I want to save the file without the space.

for example when I grep the schedule for "Team Two" from the schedule file it should have the content of Team Two schedule in the file named Team_Two
# 4  
Old 02-11-2013
Something like this?

cat data
Code:
TeamOne
Team Two
Team Three
TeamFour
Team Five O
Team Six
Team Seven
Team Forty one.

Code:
awk -F"Team[ ]*" '{print "Team_"$2}' data > result

cat result
Code:
Team_One
Team_Two
Team_Three
Team_Four
Team_Five O
Team_Six
Team_Seven
Team_Forty one.

Using the Field Separator like "Team[ ]*", will give correct split.

Last edited by Jotne; 02-11-2013 at 02:54 AM..
# 5  
Old 02-11-2013
Bash / ksh93 / zsh
Code:
while read i
do
  grep -i "$i" schedule >> "${i// /_}"
done < teams

# 6  
Old 02-11-2013
Code:
$ while read i
> do
> grep -i "$i" schedule >> "${i// /_}"
> done < teams
/usr/bin/ksh[3]: "${i// /_}": bad substitution
/usr/bin/ksh[3]: "${i// /_}": bad substitution
/usr/bin/ksh[3]: "${i// /_}": bad substitution
/usr/bin/ksh[3]: "${i// /_}": bad substitution
/usr/bin/ksh[3]: "${i// /_}": bad substitution
/usr/bin/ksh[3]: "${i// /_}": bad substitution
/usr/bin/ksh[3]: "${i// /_}": bad substitution

---------- Post updated at 07:21 AM ---------- Previous update was at 07:11 AM ----------

Thank you so much. it worked. I didnt realize you asked me to use any of the following shells
Bash / ksh93 / zsh
I used zsh and it worked.

Last edited by Scrutinizer; 02-11-2013 at 07:24 AM.. Reason: code tags
# 7  
Old 02-11-2013
Good. With other shells you could try the less efficient "$(echo "$i" | tr ' ' '_')" instead of "${i// /_}"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

2. Shell Programming and Scripting

Remove spaces from between words that are in a field

Hi all, Is there a sed/awk cmd that will remove blank space from between words in a particular field, replacing with a single space? Field containing 'E's in the example below: Example input file: AAAAA AA|BBBB|CCCCCCC|DDDDDD |EEEE EEEEEE| FFF FFFFF| ... (6 Replies)
Discussion started by: dendright
6 Replies

3. Shell Programming and Scripting

Concatenating words without spaces.

Hi All, I have written a C program to solve this problem but I am eager to know whether the same output can be obtained using sed or awk? This is the input: star ferry computer symbol prime time This is the output: starferry ferrycomputer computersymbol symbolprime primetime (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

4. Shell Programming and Scripting

grep words from output file

Hi, By using shell scripit i have save output in one file. I want to grep two words named CLUSTER and CLUSQMGR from that output file. How to grep that. output file would be having below words TYPE(QCLUSTER) ALTDATE(2010-05-17) CLUSTER(QS.CL.MFT1) ... (5 Replies)
Discussion started by: darling
5 Replies

5. Shell Programming and Scripting

Insert varying length spaces between words

Hey all, Fist post, so be kind... I have written an expect script which logs into a terminal and gathers several screens of information. Unfortunately the log file gives me all the special escape and control characters from the terminal. I am hoping to use a combination of shell scripting, sed,... (1 Reply)
Discussion started by: mpacer
1 Replies

6. Shell Programming and Scripting

how can save output?

I make shell script by use "if" statement, what should add it to shell script save which I enter it in output file txt? ---------- Post updated at 08:27 AM ---------- Previous update was at 05:59 AM ---------- I mean like this echo "enter your name" read name # now i when start... (1 Reply)
Discussion started by: Oman_Member
1 Replies

7. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

8. Programming

Counting characters, words, spaces, punctuations, etc.

I am very new to C programming. How could I write a C program that could count the characters, words, spaces, and punctuations in a text file? Any help will be really appreciated. I am doing this as part of my C learning exercise. Thanks, Ajay (4 Replies)
Discussion started by: ajay41aj
4 Replies

9. Shell Programming and Scripting

Retaining spaces between words

Retaining Spaces within a word -------------------------------------------------------------------------------- Hi Experts, I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces.... (7 Replies)
Discussion started by: RcR
7 Replies

10. Programming

How do I print(save) words to postscript?

It should be pretty simple but I'm just new to IDL and am workling through the command line and scripts(program.pro). If I want to save words to the bottom of a .ps file that I am putting a few plots in what should written in my code. set_plot works for plots but apparently not words. Print... (0 Replies)
Discussion started by: luked
0 Replies
Login or Register to Ask a Question