bash script to sort a txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script to sort a txt file
# 1  
Old 04-02-2010
bash script to sort a txt file

I am writing a script to write to and a sort txt file. After I sort the file I want to add 2 to each line of the file. My script thus far is

Code:
#!/bin/bash
cat > /ramdisk/home/stux/unsortedints.out
COUNT=0
FILE =/ramdisk/home/stux/unsortedints.out
for i in {1..100}
do
NUMBER = $[($RANDOM%1000)+1]
echo $NUMBER >> $FILE
done
sort unsortedints.out > sortedints.out


right now nothing happens when I run the script. and im unsure how to add 2, to each of the numbers.

Last edited by pludi; 04-02-2010 at 02:28 AM.. Reason: code tags, please...
# 2  
Old 04-02-2010
Code:
#!/bin/bash
FILE=/ramdisk/home/stux/unsortedints.out
#   ^__ There shouldn't be any space here !
: > $FILE # cat  unnecessary. Don't repeat the file name
COUNT=0 # Where is it used ?
for i in {1..100}
do
    NUMBER=$(( ($RANDOM%1000)+1 ))
    #    ^_^__ No spaces ! use (( )) for arithmetic expansion
    echo $NUMBER >> $FILE
done
sort -n $FILE > sortedints.out
# Use -n to sort on a numeric basis, else 10 wille be before 9

# 3  
Old 04-02-2010
thanks a lot Smilie I guess my question now would be how do I traverse through the file to add 2 to each int. I remember reading about a command in awk that did it but cant seem to find it.
# 4  
Old 04-02-2010
just the last line
Code:
(...)
sort -n $FILE | awk '{print $1 + 2}' > sortedints.out

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash incert line from 1.txt to 2.txt

i would like to insert a line from 2.txt into 1.txt between " and " or a way of adding to the end of each line " _01_ and have the numbers correspond to the line # 1.txt= foofoo "" _01_ foofoo "" _02_ foofoo "" _03_ foofoo "" _04_ 2.txt= ... (6 Replies)
Discussion started by: klein
6 Replies

2. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies

3. UNIX for Dummies Questions & Answers

Bash script to sort files

I've got a disorganized list of items and quantities for each. I've been using a combination of grep and sort to find out how much to buy of each item. I'm tired of having to constantly using these commands so I've been trying to write a shell script to make it easier, but I can't figure out how... (3 Replies)
Discussion started by: PTcharger
3 Replies

4. UNIX for Dummies Questions & Answers

Write pid and command name to a txt file while executing a bash script

Hi All, Just have a requirement, I am executing a bash shell script, my requirement is to catch the pid and job name to a txt file in the same directory, is there anyway to do it? please help me out. Regards Rahul ---------- Post updated at 08:42 AM ---------- Previous update was at... (2 Replies)
Discussion started by: rahulkalra9
2 Replies

5. UNIX for Dummies Questions & Answers

How can i sort a .txt file without loosing the header information?

Hi, I'm trying to sort 2 different .txt tab delimited files with the command line: sort -k 1b,1 inputfile > outputfile But doing that i'm also sorting the header (that ends at the end of my file). How can i sort a .txt file without sorting the header but conserving the header in the... (3 Replies)
Discussion started by: alisrpp
3 Replies

6. Shell Programming and Scripting

bash script to create txt files based on timestamp

Hi , please guide me for a bash script that will create a txt files and the name of the txt files will be as of timestamp so that each file name will be different from other and these files will be get created say after every 10 minutes in a folder(/home/p2000/sxs137), please guide me how would... (1 Reply)
Discussion started by: nks342
1 Replies

7. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

8. Shell Programming and Scripting

[bash] jump from one txt file to another

Hi all, I need to create a bash script that reads a txt file on a remote (Windows 2003) server, gets the IP-addresses out of it and then fetches the folders to copy out of another txt file. (all files are auto-generated) The IP addresses that don't have a folder_list file should be ignored. At... (31 Replies)
Discussion started by: laurens
31 Replies

9. Shell Programming and Scripting

sort txt file using another file

Hello, i have a file looks like : FILEA X,info1 Y,info2 Z,info3 T:info5 . . and another file like : FILEB groupe1: X Y T groupe2: Z (3 Replies)
Discussion started by: kamel.seg
3 Replies

10. Shell Programming and Scripting

Sorting problem "sort -k 16,29 sample.txt > output.txt"

Hi all, Iam trying to sort the contents of the file based on the position of the file. Example: $cat sample.txt 0101020060731 ## Header record 1c1 Berger Awc ANP20070201301 4000.50 1c2 Bose W G ANP20070201609 6000.70 1c2 Andy CK ANP20070201230 28000.00... (3 Replies)
Discussion started by: ganapati
3 Replies
Login or Register to Ask a Question