Register
It is currently Fri Oct 31, 2014 2:43 pm

Basic bash script - help needed


All times are UTC - 6 hours


Post new topic Reply to topic  [ 14 posts ] 
Author Message
 PostPosted: Mon Jan 10, 2011 1:21 pm   

Joined: Mon Jan 10, 2011 1:15 pm
Posts: 7
Hi!
I wonder if you can help me with very basic piece of bash script.
I want to create a script that will search in definite directory
for txt files. That files will contain some random information

for example.

John Smith
13 , Shakespeare Drive
Wandsworth
London
SW18 85D
England


And this script must copy particular lines from it

for example.

John Smith
SW18 85D


and paste it in new created text file in new directory few times.

It's how I imagine this new txt file :

John Smith
SW18 85D


Kate Smith
SD18 89D


Robert Watson
WA19 50S


So I think i will need to have in this code some kind of loop.

I had done my search and I know that commands like grep, sort and sed
will be helpful but I am rlly just a beginner and I don't have
any idea how to put it all together :/
Any advices or pieces of code?



Port Magi


Top
 Profile  
 PostPosted: Mon Jan 10, 2011 7:26 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi!

Is the information truly random, or is there any order to it?

If it's pretty regularly in the form of your sample text, with some kind of separator between records, then it should be fairly easy to sort out. If there's no separator between records, it might be a bit more difficult...


Top
 Profile YIM  
 PostPosted: Tue Jan 11, 2011 3:20 am   

Joined: Mon Jan 10, 2011 1:15 pm
Posts: 7
Hi, and thanks for response :)
It's not gonna be fully random. I didn't prepare this txt data yet.
But it's gonna definitely have the same shape so they will have some separator
or I can just made one right?


Full name : John Smith
Street : 13 , Shakespeare Drive
City : London
Street Code : SW18 85D
Bank account number : 68101010100165932222100000

==========
Full name : Mark Smith
Street : 16 , Shakespeare Drive
City : London
Street Code : SW18 85D
Bank account number : 61101010100166032222100000

==========
etc.

It's that what you mean?



EDIT (1) :

Ok I prepare pattern which will actually exist on my Linux disk
It's in Polish language, I can translate it, but I think it don't
have a influence on a bash script ?

http://www.speedyshare.com/files/26217148/data.txt

Imię oraz nazwisko : Jan Kowalski
Numer transakcji : 48569
Zakupiony produkt : Sennheiser HD 202
Naleznosc : 80 pln
Data zlozenia zamowienia : 10.01.2011
Data realizacji : 11.01.2011
Miasto : Warszawa
Ulica : Fabryczna
Nr. Mieszkania : 16
Kod pocztowy : 00-446
Numer konta bankowego : 56101010100165932222100000
===========================================


And script have to take this elements from
each person data (in primary text file there will be 10 of them) :


Imię oraz nazwisko : Jan Kowalski
Numer transakcji : 48569
Należność : 80 pln
Data realizacji : 11.01.2011
Numer konta bankowego : 56101010100165932222100000


and paste it to new text file with short intervals.


Top
 Profile  
 PostPosted: Thu Jan 13, 2011 2:49 pm   

Joined: Thu Jan 13, 2011 12:58 am
Posts: 6
Is the file that is going to be processed by the script will be in polish or english?


Top
 Profile  
 PostPosted: Thu Jan 13, 2011 3:07 pm   

Joined: Mon Jan 10, 2011 1:15 pm
Posts: 7
In polish. But i can translate key words if it is a problem.
I just turned on my virtual box and terminal, but I still dont have anything useful : /


Top
 Profile  
 PostPosted: Thu Jan 13, 2011 4:45 pm   

Joined: Thu Jan 13, 2011 12:58 am
Posts: 6
Hello, this might help:
Code:
#! /bin/bash

#################################################
#                    PolishCut                  #
#                                               #
# delimiter: used to seperate different users   #
#                                               #
#################################################


delimiter="--------------------------------------------------------"


while read
do
        if  egrep -q 'nazwisko|transakcji|Nale|data|konta' <<< "$REPLY"   # Using here strings to extract wanted fields
        then
                printf " $REPLY\n"

        elif [ "$REPLY" == "$delimiter" ]       # If read line equals delimiter in this case ------
        then
                printf "\n $delimiter \n"
        fi

done < "$1" >> "processedFile"                  # Input file(parameter) into while loop and redirects output to processedFile



Hope it helps you :).
PD: Remember you should pass your file to the script as a parameter!


Top
 Profile  
 PostPosted: Thu Jan 13, 2011 5:30 pm   

Joined: Mon Jan 10, 2011 1:15 pm
Posts: 7
Oh, you're saving my day!
Thanks for effort

I will check it right away.
So if my data are saved in data.txt
i must put in "processedFile" path to this file
for example /home/Data/data.txt right?



edit:
No i got something wrong.
processedFile is name/place for path to the NEWfile right?
So where in code i show him in which file he must do his job? :)


Top
 Profile  
 PostPosted: Thu Jan 13, 2011 5:44 pm   

Joined: Thu Jan 13, 2011 12:58 am
Posts: 6
Ok, Ill edit the script so you can specify the new file's directory, hold :).


Top
 Profile  
 PostPosted: Thu Jan 13, 2011 6:00 pm   

Joined: Thu Jan 13, 2011 12:58 am
Posts: 6
Code:
#! /bin/bash

#################################################
#                    PolishCut                  #
#                                               #
# delimiter: used to seperate different users   #
# outFile : Contains the path were the output   #
#               file will be placed             #
#                                               #
#################################################


delimiter="--------------------------------------------------------"

printf "Specify the path of the output file \n";
read  outFile

touch "$outFile"

while read
do
        if  egrep -q 'nazwisko|transakcji|Nale|data|konta' <<< "$REPLY"   # Using here strings to extract wanted fields
        then
                printf " $REPLY\n"

        elif [ "$REPLY" == "$delimiter" ]       # If read line equals delimiter in this case ------
        then
                printf "\n $delimiter \n"
        fi

done < "$1" >> "$outFile"                       # Input file(parameter) into while loop and redirects output to desired path   


This is the way to call the script:

Quote:
bash polishCut File to handle

(Obviously, I named my script polishCut, in your honor :P)

It will now ask the path of the new file that is going to be created.

Please take in mind that the directory in which the file is going to be created needs to have the adecuate writing permissions.

Also feel free to ask/appoint any possible doubt/error that might come up during the use of this script. Im glad to help


Last edited by alvaradogunner on Thu Jan 13, 2011 6:14 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Thu Jan 13, 2011 6:12 pm   

Joined: Mon Jan 10, 2011 1:15 pm
Posts: 7
Allright, i will check it out.
Thanks for this help a lot!
I must buy you a beer or smth before I die :)


Top
 Profile  
 PostPosted: Thu Jan 13, 2011 6:16 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 551
Hi,

egrep is not needed
Code:
if [[ $REPLY =~ nazwisko|transakcji|Nale|realizacji|konta ]]; then echo "$REPLY"; fi;
can replace it.
=~ evaluates regexes.

the goal of printf is to separate format and datas
Code:
printf '\n%s\n' "$separator"


Last edited by Watael on Thu Jan 13, 2011 7:16 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Thu Jan 13, 2011 6:51 pm   

Joined: Mon Jan 10, 2011 1:15 pm
Posts: 7
@alvaradogunner

Hmm I placed script file in the same folder with data.txt
and it worked. Script has created output file that
I named data2.txt
But it was empty :/

I don't see line in the script that will point him to the data.txt
Maybe it's a problem ? Or it's my fault ?

@Watael

Right now, I'm just focusing on running this sript,
but when I will deal with that i will try your idea too.
Thanks.



Ps: I hope my English is understandable. I don't have chance to use it, about a year now. So excuse me :)


Top
 Profile  
 PostPosted: Thu Jan 13, 2011 7:33 pm   

Joined: Mon Jan 10, 2011 1:15 pm
Posts: 7
Okey I add root/Dane/data.txt <<< REPLY ; after if egrep
and it's working now :)

Thanks again. You saved me a lot of time :))


@alvaradogunner
You're welcome ^^


Last edited by Port Magi on Fri Jan 14, 2011 1:02 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Thu Jan 13, 2011 8:05 pm   

Joined: Thu Jan 13, 2011 12:58 am
Posts: 6
I realised about what you're saying, so:


Code:
#! /bin/bash

#################################################
#                    PolishCut                  #
#                                               #
# delimiter: used to seperate different users   #
# outFile : Contains the path were the output   #
#               file will be placed             #
#                                               #
#################################################


delimiter="--------------------------------------------------------"

printf "Specify the path of the output file \n";
read  outFile

while read
do
        if  egrep -q 'nazwisko|transakcji|Nale|data|konta' <<< "$REPLY"   # Using here strings to extract wanted fields
        then
                printf " $REPLY\n"

        elif [ "$REPLY" == "$delimiter" ]       # If read line equals delimiter in this case ------
        then
                printf "\n $delimiter \n"
        fi

done < "$1" >> "$outFile"                       # Input file(parameter) into while loop and redirects output to desired path
                                     


BTW Thank you for telling about the =~ didnt know it worked on bash :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP