Help with an 'easy' script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with an 'easy' script
# 1  
Old 01-12-2007
Error Help with an 'easy' script

Hello guys, I need some help to solve a little problem that I have here:

I have a big file with almost 100.000 lines of data, here is an example of line:

Code:
100099C01101C00000000059399489283CREMOVISTAR_TX      010001C00000000000099069799MOVISTAR_Tx         070112114240005D070112114240005D100C00060475598801011C0C0C00100

What I need? please i need a script to change the first field (first number) from '1' to '2' ONLY when in the same line is present the word 'CREMOVISTAR_TX' (like in the example) or the word 'CREALEGRO_TX'.

Thanx and sorry for my english, I am from Ecuador Smilie

Lestat
# 2  
Old 01-12-2007
Solution

Nobody answer me in 7 hours Smilie but I finaly made it!!!! here is my solution:

Code:
cat $1 |  \
while read line
do
                awk '{ if (substr($line,34,3) == "CRE") print '2' substr($line,2,162); else print $line }' >> arreglado
done

where '$1' is the file with records to check and 'arreglado' is the new file fixed the first value.

Anyway thanx.

Lestat
# 3  
Old 01-12-2007
An another way to do it ( still with awk )

Code:
cat FILE |awk ' { if( $0 ~  "(MOVISTAR_TX|CREALEGRO_TX)" ) 
        {
        sub("^1","2")  
        }
        print $0 
}' > NEWFILE

# 4  
Old 01-13-2007
Another syntax with awk :
Code:
awk '/MOVISTAR_TX|CREALEGRO_TX/ { sub(/^1/,"2") ; print }' inputfile > outputfile


Jean-Pierre.
# 5  
Old 01-13-2007
in this case, the line witch doesn't match the regexp won't be printed
# 6  
Old 01-13-2007
Quote:
Originally Posted by becket
in this case, the line witch doesn't match the regexp won't be printed
borrowing from aigles:
Code:
awk '/MOVISTAR_TX|CREALEGRO_TX/ { sub(/^1/,"2")}1' inputfile > outputfile

# 7  
Old 01-13-2007
MySQL

vgersh99 : are you praticing for some Obfuscated code Competition ? Because this is great stuff Smilie You made me jump to my books ... yelling

For not this post be a waste of time, i'll give a little explanation on this ( out of the hat ) '1'

awk are made of motif and procedure

motif { procedure }

If motif is missing, procedure is applied to all recording
if procedure is missing, every line mathing motif will be print


And as a matter a fact, '1', '2' , should be considered as 'non false' return and by consequence print the line ...

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Easy script

Write a script, which of the directory including sources of kernel, choose files with sources in C (files with .c extension), including in name "io" with dirvers (located in random subdirectory drivers) and place their content in file FILE. (to 20 lines). Could someone help me with this task?... (1 Reply)
Discussion started by: Czabi
1 Replies

2. Shell Programming and Scripting

Opinion on an easy shell script (mv)

:wall:I've this simple code: STF=/opt/aaa cat $STF | nice sort -u > $STF.new && mv $STF.new $STF Which works until today. What happened is that this script has been corrupted the FS, so I've to use fschk to repair the filesystem. I presume the move command executed just a little too early... (1 Reply)
Discussion started by: accolito
1 Replies

3. Shell Programming and Scripting

I need help to write easy shell script

Hello every one :D I am very new in Linux ... that why I do not have any idea to write the script :confused: I am trying to read some tutorial , but I do not have enough time to do it ! because I have to submit my project results in next Wednesday I need your help to write script ! I will... (2 Replies)
Discussion started by: seereen
2 Replies

4. UNIX for Dummies Questions & Answers

Easy About Bash Script function

Hi to all, first of all,i am working on MINIX 3 OS. I want to create a bash script file,which will create a list of files(not directories) that have been modified one specific day (i.e today) under my home directory. thank you very much!! :) (8 Replies)
Discussion started by: kostis1904
8 Replies

5. Shell Programming and Scripting

Easy Checkup Script

Hi, I am planning the following to do. On my linux system I've got different users in the /home/ directory. These users have file limitations. So every user below the /home/ directory should get a text file in a seperate folder /home/$user/files/ which tells him how many files he is already... (2 Replies)
Discussion started by: crusher
2 Replies

6. Shell Programming and Scripting

My first script of day, it looks easy, but...

I've began my journey at 7:50, and at this time i've lost 40 minutes in this easy but :confused::eek:-script. Someone can help me ? I want to see the differences beetween all xml files in two directories (they must be equals), and this is my script: #!/bin/bash dir1="261108"... (5 Replies)
Discussion started by: trutoman
5 Replies

7. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies

8. UNIX for Dummies Questions & Answers

Help with an 'easy' script

Hello guys, Forgive me if I duplicate the post but i think i make a mistake choosing the correct forum. I need some help to solve a little problem, I have a big file with almost 100.000 lines of data, here is an example of line: 100099C01101C00000000059399489283CREMOVISTAR_TX ... (3 Replies)
Discussion started by: lestat_ecuador
3 Replies

9. Shell Programming and Scripting

easy script

a script, cheer that prints its parameter as shown in the example below. eg: $ cheer U N I X Give me a U! U! Give me a N! N! Give me a I! I! Give me a X! X! #!/bin/sh for letter do echo "Give me a $letter!";echo "$letter!" done this is the code i used for the above script (2 Replies)
Discussion started by: problems
2 Replies

10. UNIX for Dummies Questions & Answers

Easy script

Hi there, Could you please help me with that ? I want to grep a specific string in an .xml file and then rename the file according to the string I found. How can I do that for many files. Because all the files have similar names but different time stamps I want to name them so it's easy to... (8 Replies)
Discussion started by: guest100
8 Replies
Login or Register to Ask a Question