Can someone please help me translate this UNIX script to English?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can someone please help me translate this UNIX script to English?
# 1  
Old 04-22-2013
Question Can someone please help me translate this UNIX script to English?

Hi guys,

I'm trying to create a DataStage job and I'm basing it off an existing similar project.

The 'Input' value of the job is:

Quote:
cd /DATA/CSV_FILES; for i in *.csv ; do if [ -f "$i" ]; then sed "s/^/$i,/" "$i" > temp ; sed 's/.*/&,/' temp >temp2 ; fi ; cat temp2 ; done > output.txt; rm -f temp*
(where "/DATA/CSV_FILES" is where all the files were located)

My understanding is that for each CSV, it added the 'filename' as Column A. And then it joined all the CSV files together and created a single output.txt file.

I've copied everything that this job did - only that my CSVs are slightly different. Unfortunately when I run the job, it does not work and tells me that 'output.txt' is not in the folder. So I'm guessing I need to tweak this UNIX code a bit to suit my needs.

Problem is I don't understand it. Is anyone out there possible to give me some guidance on how to do this? (or better yet, translate it to English please? Smilie )

Kind regards
# 2  
Old 04-22-2013
Please use code tags, not quote tags, for code!

Code:
cd /DATA/CSV_FILES                      # change dir
for i in *.csv                          # loop for all files with .csv extension
  do if [ -f "$i" ]                     # if regular file
       then sed "s/^/$i,/" "$i" > temp  # prefix every line with respective file name
            sed 's/.*/&,/' temp >temp2  # suffix every line with comma
     fi
  cat temp2                             # list the file to stdout
  done > output.txt                     # redirect all above (= every .csv prefixed) to file 
rm -f temp*                             # remove tmp files

# 3  
Old 04-22-2013
I guess the problem lies here:
Code:
cd /DATA/CSV_FILES

Verify if this path is valid. I would suggest to replace it with absolute path.
# 4  
Old 04-22-2013
Here is a similar variation. I've taken the liberty to edit some.
Code:
cd /DATA/CSV_FILES # Get in correct directory
rm -f output.txt   # Clear output file
for i in *.csv; do       # For each input .csv file
  if [ -f "$i" ]; then   # If the file is a "regular" file (test not really needed)
    sed "s/^/$i,/" "$i" > temp          # Add file name as first column for all lines and write to output.txt
    sed 's/.*/&,/' temp >> output.txt    # Do you really want another comma at the end of the line?
  fi
done
rm -f temp


Last edited by hanson44; 04-22-2013 at 03:50 AM.. Reason: > for >>
# 5  
Old 04-22-2013
Sorry about that - will wrap in code next time!
Cheers for the translation as well
# 6  
Old 04-23-2013
What about (untested!):
Code:
sed  "s/^/$i,/ ; s/.*/&,/" /DATA/CSV_FILES/*.csv > /DATA/CSV_FILES/output.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Translate from english to french

Hi, I wrote a script to convert a given word from English to French. But I am not able to figure out what I am missing here. I am not able to get the translated word Below is my script: French=/root/dict/entofr.txt for i in $* do word="echo $word $i" done while: do cat <<... (1 Reply)
Discussion started by: pinky7630
1 Replies

2. Shell Programming and Scripting

translate a short csh script to bash

Hi, I have a csh: set NODES = `cat $HOST_FILE` set NODELIST = $TMPDIR/namd2.nodelist echo group main >! $NODELIST foreach node ( $NODES ) echo host $node >> $NODELIST end @ NUMPROCS = 2 * $#NODES I am very frustrated to translate it to bash: NODES = `cat... (3 Replies)
Discussion started by: rockytodd
3 Replies

3. UNIX for Dummies Questions & Answers

translate to normal english

lnode * head = temp; (1 Reply)
Discussion started by: rickym2626
1 Replies

4. Shell Programming and Scripting

how to achicve translate in unix

hi, i have file with 4 columns, the first column contains 10 numbers. i want to replace t the numbers (u can say i want to use cryptography). i want to replace 1 with 4, 2 with 5, 3 with 9..... how can i achieve this in unix. :confused: (1 Reply)
Discussion started by: javeed7
1 Replies

5. UNIX for Dummies Questions & Answers

Trouble with UNIX tr (translate) function

UNIX script - problem. I want the spaces in my Item variable to be replaced with a question mark. Can you tell me what I am doing wrong? This is the whole code line. Item | tr -s " " "?" Why is this not making any changes to the Item value? Thanks for any help you can give! tg (2 Replies)
Discussion started by: by_tg
2 Replies

6. OS X (Apple)

Cannot translate Unix executable files

Help!! I loaded OS X Panther on my Mac G4 and found that many files previously saved as txt files were inadventently converted to Unix executable files. When I try to read these in Word, the Word filters cannot recognize or translate the file properly. Does anyone know how to translate these files?... (1 Reply)
Discussion started by: Steven Greenber
1 Replies

7. UNIX for Dummies Questions & Answers

describe unix commands in english

the following unix command ls | grep'?cw1' | wc -l converting it to english is it going to be like list the result of the search '?cw1' in number of lines is that correct ? (4 Replies)
Discussion started by: props
4 Replies

8. Shell Programming and Scripting

Please decode in English

Hello: Can anyone please decode this script in English. I have also made some comments which I know.. The actual script does not have one comment also.. #! /bin/ksh . odbmsprd_env.ksh #setting the env.. echo $0 Started at : `date '+%d-%m-%Y %H:%M:%S'` # what's echo $0 ... (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question