The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help required with a Csh script to read data from a file fizzme Shell Programming and Scripting 1 05-29-2008 08:30 PM
Need to read data from a file (cut/awk) rejirajraghav Shell Programming and Scripting 1 04-29-2008 04:13 PM
Post Shell programming: Question about source a file and read data from the file ccwq Shell Programming and Scripting 3 08-04-2007 11:28 PM
Read from data file fongthai Shell Programming and Scripting 12 02-27-2007 09:22 PM
Read data from a file into a variable yorkdg Shell Programming and Scripting 2 12-09-2004 05:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-04-2008
wakhan wakhan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 58
Read data from one file and putting in new file ??

Hello All,

I have a file which contain data something like this:


CELL 2 TEST AND DIAGNOSTIC UNIT 2
CELL 2, CDM 1 CBR 1 TRANSMIT PORT (TXPORT) 1
CELL 2, CDM 1 CBR 2 TRANSMIT PORT (TXPORT) 1
CELL 2, CDM 1 CBR 3 TRANSMIT PORT (TXPORT) 1
CELL 2, CDM 1 CBR 1 TRANSMIT PORT (TXPORT) 2
CELL 2, CDM 1 CBR 2 TRANSMIT PORT (TXPORT) 2
CELL 2, CDM 1 CBR 3 TRANSMIT PORT (TXPORT) 2

CELL 3 PACKET PIPE (PP) 3
CELL 3, CDM 1 CBR 3 TRANSMIT PORT (TXPORT) 1

CELL 4 TIME FREQUENCY UNIT (TFU) 1
CELL 4 TEST AND DIAGNOSTIC UNIT 2
CELL 4, CDM 1 PRIMARY SIGNALING LINK
CELL 4, CDM 1 ALTERNATE SIGNALING LINK
CELL 4 PACKET PIPE (PP) 1
CELL 4 PACKET PIPE (PP) 2
CELL 4 PACKET PIPE (PP) 3

What i want is that i read that file and those lines which starts from cell 2 copies and paste into new file named cell2 in the same directory, same as for cell 2 and cell 3 and 4. Its mean that 3 new files will be created named cell2,cell3 & cell4.

Any idea how i can do this ??

Regards,
Waqas Ahmed
  #2 (permalink)  
Old 12-04-2008
peter84ol peter84ol is offline
Registered User
  
 

Join Date: Apr 2008
Location: Poland/Olsztyn
Posts: 3
cat cell | grep "CELL 2" > cell2
or
sed -n -e "/CELL 2/p" cell > cell2
or
awk '/CELL 2/{print $0}' cell > cell2
  #3 (permalink)  
Old 12-04-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

the following script reads in the file line by line, then extracts
the filenumber from string, constructs the corresponding filename
and prints the matching information to this file. Blank lines are
delete.


Code:
while read line 
do 
[[ $line = *CELL* ]] \
&& FILENUMBER=$(sed 's/CELL \([0-9]\+\)[ ,].*/\1/' <<< $line) \
&& echo $line >> cell${FILENUMBER} 
done < file

HTH Chris
  #4 (permalink)  
Old 12-04-2008
wakhan wakhan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 58
Hello All,

I used this : cat cell | grep "CELL 2" > cell2 to read file named cell and to grep only lines that starts from cell 2. But when i open the output file cell2 so it also contain lines starting from cell 21 22 23 to 29.
But my requirement is that output file cell2 only contains lines that starts from cell 2 not from cell21 to 29.
I hope you understand what i am saying.

How i can do that?

Regards,
Waqas Ahmed
  #5 (permalink)  
Old 12-04-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Try this:


Code:
awk -F" |," 'NF{print > tolower($1$2)}' file

Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
  #6 (permalink)  
Old 12-04-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

firstly: you don't need cat: grep "pattern" file is enough.
secondly: your regexp search for all patterns matching "CALL 2"
at "any position of any length".


Code:
grep "^CELL 2[ ,]" file

Will match only lines starting with "CELL" followed by "2" and a space
or comma.

Try my solutions. It should give you what you want. Save it in a
file a make it executable or run it from the command line.
  #7 (permalink)  
Old 12-04-2008
wakhan wakhan is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 58
Hello Christoph Spohr

Can you please tell me the usage procedure of your code:


Code:
while read line 
do 
[[ $line = *CELL* ]] \
&& FILENUMBER=$(sed 's/CELL \([0-9]\+\)[ ,].*/\1/' <<< $line) \
&& echo $line >> cell${FILENUMBER} 
done < file

where i have to put the filename which will be read.

i save your code and make it executable but its giving below error:
KarachiOMP root> ./co
./co: syntax error at line 6: `FILENUMBER=$' unexpected
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0