Combine several commands in a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine several commands in a bash script
# 1  
Old 10-12-2013
Combine several commands in a bash script

Hi all,

I have large files with url-s ending on "|<number>" which is the Page Rank for the website as shown in the example below
http://www.machinokairo.com/2012/05/post-39.html|2

I am using "grep" to sort out all url-s in a particular way: first, remove all ending on "|0" and write the output to a file, then remove all ending on "|1" and write the output to a new file and so on up to "|5". Each time I remove certain PR and have the rest in separate file. For now I use the following commands to do that

grep --invert-match "|0" sitelist > sitelist_PR1.txt
grep --invert-match "|1" sitelist_PR1.txt > sitelist_PR2.txt
.
.
grep --invert-match "|5" sitelist_PR5.txt > sitelist_PR6.txt


I will appreciate if someone helps me with a bash script to perform all of the above
# 2  
Old 10-12-2013
A character set catches the whole range: "|[0-5]"
# 3  
Old 10-12-2013
Quote:
Originally Posted by MadeInGermany
A character set catches the whole range: "|[0-5]"
yes, right. but how to implement all these in a bash script using "grep" or "sed" commands and still have all separate output files?
# 4  
Old 10-12-2013
awk OK? Try something like:
Code:
awk -F\| '/http:/{print $1 > ("sitelist_PR" $2 ".txt")}' file

# 5  
Old 10-12-2013
Code:
grep -v "|0" sitelist | tee sitelist_PR1.txt |
grep -v "|1" | tee sitelist_PR2.txt |
.
.
grep -v "|5" > sitelist_PR6.txt

# 6  
Old 10-12-2013
Code:
awk -F\| '/http:/{print $1 > ("sitelist_PR" $2 ".txt")}' file

Thank you for your input.
Command works in a bit different way that I need and
creates output files from sitelist_PR .txt to sitelist_PR9 .txt, actually I needed only to PR.6, but that is fine.

There are two things however that are different from the desire output:
- each file contains only url-s with PR same as in the filename i.e. sitelist_PR1 .txt contains url-s with PR1 only - my goal was to remove those url-s and have all the rest higher than PR1 in this file;
- when I look at the file name I see blank space before .txt

---------- Post updated at 02:33 PM ---------- Previous update was at 02:29 PM ----------

Quote:
Originally Posted by MadeInGermany
Code:
grep -v "|0" sitelist | tee sitelist_PR1.txt |
grep -v "|1" | tee sitelist_PR2.txt |
.
.
grep -v "|5" > sitelist_PR6.txt

Thank you for your efforts but I really need a script or one line command. I have already tried the following

Code:
#!/bin/bash

for PR in {0..5} ; do
 grep --invert-match "|${PR}$" sitelist.txt > sitelist_PR${PR}.txt
done

but unfortunately it creates only empty files
# 7  
Old 10-12-2013
Save whatever commands to a file, and you can run it with
Code:
bash file

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using commands within bash script

The purpose of enclosed script is to execute selected command and output success or failure in whiptail msgBox Works as expected when command returns zero to msgBox. I cannot figure out how to continue / automate my script when command expects reply to continue / terminate. I am doing it... (2 Replies)
Discussion started by: annacreek
2 Replies

2. Shell Programming and Scripting

Combine 2 Commands

Hello, I have the following code. I wonder if it can be combined into 1 command. y=`ls -1| tail -n 1` m=${y%.abc} Thank you. (3 Replies)
Discussion started by: april
3 Replies

3. Shell Programming and Scripting

Combine multiple commands

I have the following sh-script: konsole -T todo -e vi todo.txt & konsole -T window1 -e ssh user@server & konsole -T window2 -e ssh user@server2 -e cd directory & The first two lines are working fine. The first opens a txt-file, the second opens a ssh-connection. The third line... (6 Replies)
Discussion started by: andre666
6 Replies

4. Shell Programming and Scripting

How to combine 2 bash script into 1?

Hi all, I’ve quick question on bash scripting. Here is my initial input. This is just an example. The value could be different numbers/ip addresses. host.txtFinal output should be like this. output-final.txt In order to have this, I’ve 2 different scripts. The first one is just to... (6 Replies)
Discussion started by: type8code0
6 Replies

5. Shell Programming and Scripting

How to create an executable bash script for these commands?

I wish to create an executable bash script that will run the following commands as root, that is, using sudo su iptables-save | awk '/^ / { print $1 } /^:+ / { print $1 " ACCEPT" ; } /COMMIT/ { print $0; }' | iptables-restoreMy first attempt at bash... (9 Replies)
Discussion started by: thixeqi
9 Replies

6. Shell Programming and Scripting

How to combine awk and bash commands in script ?

Dear friends, I am just trying write one script using 2 files one file will contain details like below #X SERVER X LOCATION URL="http://www.abcd.com" FILENAME="abc.txt" ID_NAME="myabc_xyz" SERVER_PATH="/usr/local/dummy/html/....." #Y SERVER Y LOCATION URL="http://www.xyz.com"... (10 Replies)
Discussion started by: Akshay Hegde
10 Replies

7. Shell Programming and Scripting

Can I combine these two commands into one?

sed -e :a -e 's/<*>//g;/</N;//ba' a2.html -removes html tags and sed -i 's/YOURS TRULY/Joe Bob/' a2.html Replaces a string with another string can i make it into one string? (2 Replies)
Discussion started by: boyboy1212
2 Replies

8. Shell Programming and Scripting

Execute ssh commands through bash script

Hi all! I am trying to write a script that will check if a certain directory is available at several different nodes and then do stuff in it ..... On the beginning of the script I give as a variable the directory and the number of the nodes and then I loop like this: for... (3 Replies)
Discussion started by: idet2
3 Replies

9. Shell Programming and Scripting

how to run non-standard commands in bash script?

Hello All. I suspect that this will be a clear noob question, but I haven't been able to figure it out using the usual methods, so I turn to you. I've written a script to create input files for the quantum chemistry program NWCHEM. Generally you create an input file and then execute it by... (12 Replies)
Discussion started by: EinsteinMcfly
12 Replies

10. UNIX for Dummies Questions & Answers

Combine commands

Hi, i tried to combine grep with find and it didnt work grep 'find dirname filename" i also would like that the file will be sorted in the way. thanks a lot. (2 Replies)
Discussion started by: Spoiler
2 Replies
Login or Register to Ask a Question