Help with Bash Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Bash Shell Script
# 8  
Old 07-10-2009
Quote:
Originally Posted by khanvader
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

First of all, there's no need for 'ls' - this is useless.
Quote:
Originally Posted by khanvader
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.
No, 'mv' does the MOVE - that's why it's called 'mv' and not 'cp'.
If you want to 'copy', use 'cp'.
Quote:
Originally Posted by khanvader
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
Hmm.... I don't see any mention of 'test.csv' in the script. Maybe somebody just 'mv'-ed that file while the script was running.
Uncomment the 'set -x' to see the script debug output.
Code:
#!/bin/bash
#set -x
for i in cliaDir/*
do
  myFile=$i
  echo "$myFile"
  nawk '{
     if (out) close(out)
     out=$1 ".txt"
     print >> out }' "${myFile}"
  mv $i processedDir/
done

# 9  
Old 07-10-2009
Actually...the script is working for me now. The only change made was the one where u suggesting to not use 'ls' in the for loop. MV is what I needed and not CP so I am clear on that but it was not working for me for some reason but it is now.

Now with the same script if I wanted to check the existence of a file in cliaDir/, can you help me with that? In other words, the script should check to see if file is in cliaDir/ or don't run the script.
# 10  
Old 07-13-2009
Skip first header row before splitting

Here is the code so far that I got working with good help from the experts. It is getting the file from the directory and splitting to create files by first column. However I need to skip the first row since it is has header columns.

Code:
#!/bin/bash
#set -x
for i in cliaDir/*
do
  myFile=$i
  echo "$myFile"
  nawk '{
     if (out) close(out)
     out=$1 ".txt"
     print >> out }' "${myFile}"
  mv $i processedDir/
echo "here is the file name $i"
done

Any ideas, I would appreciate.

Last edited by vgersh99; 07-13-2009 at 01:22 PM.. Reason: code tags, PLEASE!
# 11  
Old 07-13-2009
Code:
#!/bin/bash
#set -x
for i in cliaDir/*
do
  myFile=$i
  echo "$myFile"
  nawk 'FNR>1{
     if (out) close(out)
     out=$1 ".txt"
     print >> out }' "${myFile}"
  mv $i processedDir/
echo "here is the file name $i"
done

# 12  
Old 07-16-2009
Slitting of file by first column

This is working great for me except that I am not able to move the split files into some other directory.

Within nawk, it splits great by first colulmn but the file is getting saved in the directory where I am running this script from....I would like to send it to some other directory. How can I achieve that?
# 13  
Old 07-16-2009
Quote:
Originally Posted by khanvader
This is working great for me except that I am not able to move the split files into some other directory.

Within nawk, it splits great by first colulmn but the file is getting saved in the directory where I am running this script from....I would like to send it to some other directory. How can I achieve that?
Code:
#!/bin/ksh

targetDir='/path/to/some/other/dir'
for i in cliaDir/*
do
  myFile=$i
  echo "$myFile"
  nawk -v target="${targetDir}" '
     FNR>1{
     if (out) close(out)
     out=target "/" $1 ".txt"
     print >> out }' "${myFile}"
  mv $i processedDir/
echo "here is the file name $i"
done

# 14  
Old 07-17-2009
vgersh99...thanks again for your help...it works great for me now :-)
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