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

cat cell | grep "CELL 2" > cell2
or
sed -n -e "/CELL 2/p" cell > cell2
or
awk '/CELL 2/{print $0}' cell > cell2

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.

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

HTH Chris

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

Try this:

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

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

Regards

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".

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.

Hello Christoph Spohr

Can you please tell me the usage procedure of your 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

Have you tried my solution?

Regards

Use Franklin's solution, it is much better.

I suppose you shell doesn't support $(...). So you have to
replace it with `...` (backticks.)

To elaborate on Chis's code:

Command Substitution

Command substitution allows the output of a command to replace the command name. There are two forms:

$\(command\) 

or

\`command\` 

EXAMPLE:

#!/bin/bash
MYFILES=$(ls -d ~/*)
MYFILES2=`ls ~`

echo "$MYFILES"
echo "------------------------------------"
echo "$MYFILES2"

will give output like:

dirA dirB
----------------------------------
dirA dirB file1 file2 file3

Can you please explain how to use below code:

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

where i have to define the input file and what will be the name of output file?

i put the below line in file and make it executable so the following error occurs.

all is input file name:

#!/bin/sh
awk -F" |," 'NF{print > tolower($1$2)}' all

KarachiOMP root> ./code3
awk: syntax error near line 1
awk: bailing out near line 1

Regards,
Waqas Ahmed

As I mentioned, use nawk or /usr/xpg4/bin/awk on Solaris.
If you want the output in a file redirect the ouput of the command to the file.

Regards

Now i am using nawk and its giving below error. all is the name of my input file.

#!/bin/sh
nawk -F" |," 'NF{print > tolower($1$2)}' all

KarachiOMP root> ./code3
nawk: null file name in print or getline
 input record number 5, file all
 source line number 1

all contain data something like this.

KarachiOMP root> cat all
OP:ALARM,ALL!
 PF

M 54 OP:ALARM
     CELL 3 PACKET PIPE (PP) 3
     CELL 3, CDM 1 CBR 3 TRANSMIT PORT (TXPORT) 1

     CELL 4 PACKET PIPE (PP) 1

     CELL 5, CDM 1 ANTENNA DIVERSITY IMBALANCE 3
     CELL 5 RECEIVE AMPLIFIER FAILURE 4
     CELL 5 RECEIVE AMPLIFIER FAILURE 6

     CELL 6 RECEIVE AMPLIFIER FAILURE 2

     CELL 7, CDM 1 CBR 2 TRANSMIT PORT (TXPORT) 1

     CELL 10, CDM 1 ANTENNA DIVERSITY IMBALANCE 3
     CELL 10 RECEIVE AMPLIFIER FAILURE 5

The given format of the file is different from the first post, try this:

nawk '/CELL/{sub(",","",$2);$1=$1;print > tolower($1$2)}' file > newfile

Thanks Franklin52 for your Great HELP, now code is working with my file.

Can you please check the thread named below in Shell Programming and Scripting, Actually i having problem in that too, actually i want command output in new fie but complete output is not coming in output file.

Please check this thread and help if you can.

Getting command output and putting into newfile

Regards,
Waqas Ahmed

Hello All,

nawk '/CELL/{sub(",","",$2);$1=$1;print > tolower($1$2)}' file > newfile

INPUT FILE
KarachiOMP root> cat all
OP:ALARM,ALL!
 PF

M 54 OP:ALARM
     CELL 3 PACKET PIPE (PP) 3
     CELL 3, CDM 1 CBR 3 TRANSMIT PORT (TXPORT) 1

     CELL 4 PACKET PIPE (PP) 1

     CELL 5, CDM 1 ANTENNA DIVERSITY IMBALANCE 3
     CELL 5 RECEIVE AMPLIFIER FAILURE 4
     CELL 5 RECEIVE AMPLIFIER FAILURE 6

     CELL 6 RECEIVE AMPLIFIER FAILURE 2

     CELL 7, CDM 1 CBR 2 TRANSMIT PORT (TXPORT) 1

     CELL 10, CDM 1 ANTENNA DIVERSITY IMBALANCE 3
     CELL 10 RECEIVE AMPLIFIER FAILURE 5

I have one more question as the above code creates multiple files according to cell * numbers.

What if i have the same data (as input file) merge with many other lines in the log file and i want to just get those line which start from word CELL and then put all the data in single file. and when the word cell 86 comes so the script stops and output file creates containing same data as input file from cell 1 to cell 85

Regards,
Waqas Ahmed

nawk '/CELL 86/{exit}/CELL/{$1=$1;print}' file > newfile

Regards

nawk '/CELL 86/{exit}/CELL/{$1=$1;print}' LOG > newfile

Above code is also getting other unwanted lines from LOG file. I just want those files which start from word CELL like this:

CELL 83, ASMB 1 TRANSMITTER AMP INDETERMINATE 5
CELL 83, ASMB 1 TRANSMITTER AMP INDETERMINATE 7
CELL 83, ASMB 1 TRANSMITTER AMP INDETERMINATE 8
CELL 83 RECEIVE AMPLIFIER INDETERMINATE 1

But above code is also getting lines like this:
A 46 REPT:CELL 65 CP FAILURE, UNANSWERED ORIGINATION
A 46 REPT:CELL 35 CP FAILURE, ANSWERED TERMINATION
A 46 REPT:CELL 7 CP FAILURE, UNANSWERED ORIGINATION

Actually i want to tell you the exact story what i am try to do, one other thread is also going on that issue so please dont consider it duplicate thread issue.

i am running below code:

/omp/bin/TICLI "op:alarm,all" &
nawk '/CELL 86/{exit}/CELL/{$1=$1;print}' /omp/omp-data/logs/OMPROP1/081207.APX > all

what my requirement is get complete output of this command:
/omp/bin/TICLI "op:alarm,all"

in output file, but when i run this:
/omp/bin/TICLI "op:alarm,all" > outputfile
so always different amount of data comes in my file but never complete.

when i run that command so the output of the command is also generated in our LOG file so now i am trying to extract the output of that command through log file.

But because log file contains data of whole system and many other commands so i need some mechanism that at the same time i run command on the shell and the output of sametime comes in output file.

Regards
Regards,
Waqas Ahmed

Try this:

nawk '{$1=$1}/^CELL 6/{exit}/^CELL/' LOG > newfile

No this code is not giving any output in output file.

/omp/bin/TICLI "op:alarm,all" &
nawk '{$1=$1}/^CELL 6/{exit}/^CELL/' /omp/omp-data/logs/OMPROP1/081208.APX > all

Below code is giving output in output file but it is also getting other unwanted lines from LOG file.

/omp/bin/TICLI "op:alarm,all" &
nawk '/CELL 86/{exit}/CELL/{$1=$1;print}' /omp/omp-data/logs/OMPROP1/081208.APX > all

I just want those files which start from word CELL like this:
Code:

CELL 83, ASMB 1 TRANSMITTER AMP INDETERMINATE 5
CELL 83, ASMB 1 TRANSMITTER AMP INDETERMINATE 7
CELL 83, ASMB 1 TRANSMITTER AMP INDETERMINATE 8
CELL 83 RECEIVE AMPLIFIER INDETERMINATE 1

But above code is also getting lines like this:

A 46 REPT:CELL 65 CP FAILURE, UNANSWERED ORIGINATION
A 46 REPT:CELL 35 CP FAILURE, ANSWERED TERMINATION
A 46 REPT:CELL 7 CP FAILURE, UNANSWERED ORIGINATION

Regards,
Waqas Ahmed