Help with Bash Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Bash Shell Script
# 1  
Old 07-08-2009
Help with Bash Shell Script

I am fairly new to Unix scripting. A need has come where I need to to read the first column of a tab delimited file. The first column will be an id number and so I would need to read the first column one by one and create another file with only unique id numbers. So for example, if I have the following in my input file
12345 ABC
12345 XYZ
45678 ABC
12345 BVC
78906 ABC
12345 ABC

Here I need to create a file that should have the following:
12345
45678
78906

Thanks for the help in advance.
# 2  
Old 07-08-2009
Code:
nawk '!a[$1]++{print $1}' myFile

# 3  
Old 07-08-2009
Vgarsh,

Thanks for the quick response and yes this works exactly how I wanted. I don't understand each and every piece of the command clearly but I can look those up :-)

However, at command line this is working great but if I wanted to create a bash shell script to be able to run as part of a cron job, how would file reading part work?
# 4  
Old 07-08-2009
Code:
$ echo 'Vgarsh' | sed 's/a/e/'
Vgersh

I guess, I don't understand the question. Just create a script - like so:
Code:
#!/bin/ksh
myFile='/absolute/path/to/the/file/to/be/parsed'
nawk '!a[$1]++{print $1}' "${myFile}"

I'm not what else should be in the script - outputting the unique numbers from the script invoked from cron does not make much sense. You probably want to do something else with the those unique numbers.
# 5  
Old 07-08-2009
Sorry for misspelling your username :-)

Yes, you are right the purpose of the cron job is not just outputting file with unique numbers, I wanted to actually try it myself too but here is really what I am trying to do.

We will get a tab delimited file, the first column would have id numbers (like I mentioned earlier), we would need to create files based on unique id number. So the example I used earlier, we will create 3 files and move the records in that file. (idnumber1.txt, idnumber2.txt etc). These id numbers will mostly repeat but there could always be two or three different ones and hence, we create and move records for that id number in that file.
In other words, the idea is to split the file based on the first column in the file when it changes. This script can then be scheduled as a cron job to run at different time to look for a new input file.
# 6  
Old 07-08-2009
Code:
#!/bin/ksh
myFile='/absolute/path/to/the/file/to/be/parsed'
nawk '{
  if (out) close(out)
  out=$1 ".txt"
  print >> out }' "${myFile}"

# 7  
Old 07-10-2009
Thanks for the code...here is my current script and I have some issues that maybe you or someone can point out:

Code:
#!/bin/bash
for i in $(ls cliaDir/)
do
  myFile=$i
  echo "$myFile"
  nawk '{
     if (out) close(out)
     out=$1 ".txt"
     print >> out }' "${myFile}"
  mv $i processedDir/
done

cliaDir/ will have the file in it that would need to be split based on the first id column (which the code is doing fine)
the mv command is really copying the file in processedDir/ but not actually moving. Since the original is still in cliaDir and should not there anymore.

Another problem is that when I run the script again to process same file that is in cliaDir/, I get the following error:
test.csv
nawk: can't open file test.csv
source line number 4

Last edited by vgersh99; 07-10-2009 at 02:20 PM.. Reason: code tags, PLEASE!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Shell Programming and Scripting

Need help with bash shell script

I need to create digit day script that takes a single numeric argument and then it should print out the day of the week using the number modulo 7 formula e.g: 0 - Sunday 6- Saturday 131 - Friday I am fairly new to unix so I don't know how to use the number modulo 7 formula. Does the script need... (3 Replies)
Discussion started by: lukefrost96
3 Replies

4. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

5. Shell Programming and Scripting

Bash Shell Script

HELP!My program ends after entering one choice---need help making it take multiple inputs,instead of terminating after displaying just one #!/bin/bash# Crude address databaseclear # Clear the screen.echo " Contact List"echo " ------- ----"echo "Choose one of the following... (6 Replies)
Discussion started by: help123
6 Replies

6. Shell Programming and Scripting

Help with bash shell script

Hi, I have a file in which records contains non ascii characters. The records are comma delimited and quoted. The non ascii characters are found in a particular column. Example records "YY","AK000021","Ã","IO","PP" "Y1","AK000022","Ã","PO","PP" "Y2","AK000022","Ã","PO","PP" I need to... (2 Replies)
Discussion started by: akshu.agni
2 Replies

7. Shell Programming and Scripting

Bash shell script- help

I need to invoke a program on remote server using ssh in a shell script. In addition i would like to capture date/time and if there is any errors , then script should write to log file. can someone please help me out? (1 Reply)
Discussion started by: sam101
1 Replies

8. Shell Programming and Scripting

Bash Shell script--need help

Hi all, i am beginner to unix and trying out a shell script which does the following. i have to calculate a persons salary. his salary is read from the keyboard. he has two types of deductions. 40% as dearness allowance and 20% as house rent. i have to print the gross salary. here is the code... (5 Replies)
Discussion started by: Irishboy24
5 Replies

9. UNIX for Dummies Questions & Answers

Bash shell script

Hi Guys, I am trying to alter a script for my company. I need the start of it to go something like this. User is asked to input 8 numbers 8 numbers are written to a txt file ***** ***** ***** txt file is read ***** ***** The text file gets read in between other files represented by... (2 Replies)
Discussion started by: outthere_3
2 Replies

10. UNIX for Dummies Questions & Answers

need help with bash shell script

Hi guys! I have just started with shell programming!! I am having pronblem with variable subsitutuion. when i do egrep "*" marks this will give me the pattern match. but how can i catch the output of that result in a variable. if i say result = egrep "*" marks it gives me syntax... (2 Replies)
Discussion started by: vmtailor
2 Replies
Login or Register to Ask a Question