tr, sed, awk, cat or scripting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tr, sed, awk, cat or scripting
# 1  
Old 01-26-2009
tr, sed, awk, cat or scripting

I need to change all Newline caracters (\12) to Fieldseparator(\34).
tr -A '\12' '\34' <file1> file2

Replace all delete (\177) with Newline (\12)
tr -A '\177' '\12' <file2> file3

Put the name of the file first in all rows.
awk '{printf "%s\34%s\n", FILENAME,$0} file3 > file4

So far no problem. This works fine when I just need to do it with one file. Now I need to do it on 100 files. So I cant change the filename(file1>file2), and after doing all the tr commands and the awk command I need to concatenate them with:

cat * > AllFilesAfterTrandAwk

Here is an example:

file1 contains:
row1 \12
row2 \177

file2 contains:
row1 \12
row2 \177
row3 data

I need:
file1 row1 \34
file1 row2 \12
file2 row1 \34
file2 row2 \12
file2 row3 data

Does anyone have a clue?

Thanks,
MrKlint
# 2  
Old 01-26-2009
Try this, see if it works:

for file in *; do
(do your translation here) < $file > temp.file
mv temp.file $file
done
# 3  
Old 01-26-2009
Sorry, I do not follow. I havent worked with Unix before. I put my commands one by one in the prompt. If you have the time can you write the commands exactly as I'm supposed to write them?

what does $file mean? all files in the folder? like * ??
# 4  
Old 01-26-2009
Tools

You probably want to do some kind of loop.
Note that there are some simpler ways of doing this, I merely took your flow and modified.
For instance, the output of each awk could >> (append) to you final file, saving a step.

Code:
# create a listing of the files to work on
# you might not need to do this, if the list already exists
ls *.txt >tmp_file_list

while read tmp_file
  do
  tr -A '\12' '\34' <${tmp_file} | tr -A '\177' '\12' | awk -v FILENAME=${tmp_file} '{printf "%s\34%s\n", FILENAME,$0}' >${tmp_file}_2
done<tmp_file_list

# concatenate all the files
cat *_2 > AllFilesAfterTrandAwk

# 5  
Old 01-26-2009
I guess this is scripting. I have never done that before, but I will try to find out how to do it.

Thanks.
# 6  
Old 01-26-2009
Tools

Scripting is merely executing commands that you could type in directly. You could have put your original example into a script "as-is".

Mostly, there are exceptions, anything you type directly from
Code:
ls -l

to
Code:
awk .....

can be put inside a script file.

Note: after you save your command file, often saved with a .sh ending as in convert12.sh , you need to make the file executable with a
Code:
chmod +x convert12.sh

command.
# 7  
Old 01-30-2009
This is the solution I came up with...

#!/usr/bin/sh
# Create list
ls F4* > tmp_file_list

# Reads the list and does the restructuring
while read tmp_file_list
do
tr -A '\12\177' '\11\12' <${tmp_file_list}> ${tmp_file_list}_1
awk '{printf "%s\11%s\n",FILENAME,$0}' ${tmp_file_list}_1 > ${tmp_file_list}_2
done < tmp_file_list

# Concatenate all the files
cat F4*_2 > AllFilesAfterTrAndAwk
rm F4*_1
rm F4*_2
rm tmp_file_list
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Working with strings (awk, sed, scripting, etc...)

Hi evrybody For those who are bored I suggest exercise for tail :) There is "csv" string: A,B,C,D,E,G Desired output: | (A) A | (A,B) B | (A,B,C) C | (A,B,C,D) D | (A,B,C,D,E) E | G There are no whitespace characters at the beginning and end of the line. (7 Replies)
Discussion started by: nezabudka
7 Replies

2. Shell Programming and Scripting

Need to add prefix using sed or awk from cat the file

I need the use sed or AWK using cat the file Node1 TDEV RW 1035788 TDEV RW 1035788 Server1 TDEV RW 69053 Server2 TDEV RW 69053 TDEV RW 103579 Server3 TDEV RW 69053 server4 RDF1+TDEV RW 69053 RDF1+TDEV RW 517894 RDF1+TDEV RW 621473 server6 TDEV RW 34526 TDEV RW 34526 (22 Replies)
Discussion started by: ranjancom2000
22 Replies

3. Shell Programming and Scripting

Help shell scripting using awk or sed or other

I need to create a script to change a file depending of 3 conditions using a target as parameter... first condition <chamada> <numeroTerminalOriginador>CALLER</numeroTerminalOriginador> <imeiOriginador></imeiOriginador> <cgiPrimeiraErbOriginador></cgiPrimeiraErbOriginador>... (2 Replies)
Discussion started by: poulis
2 Replies

4. Shell Programming and Scripting

Need help with awk and sed scripting

I need help with sed and awk scripts to search for Symmetrix ID=000090009902 and then grep its child disk devices associated to the dead paths and display them only, so that those dead devices can be removed. test01:/#powermt display dev=all Pseudo name=hdiskpower0 Symmetrix ID=000090009902... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

5. Shell Programming and Scripting

Most vexing: Sed or Awk scripting for date conversion needed

Hi, I have some files being sent to me that have dates in them in this format: from 1/8/2011 15:14:20 and I need the dates in this format (mysql date format) To 2011-01-08 15:14:20 all I have so far is the regexp that detects the format: sed -r -e 's@\1/\2/\3\4\5\6]::$@do... (7 Replies)
Discussion started by: Astrocloud
7 Replies

6. Shell Programming and Scripting

Scripting awk or sed or shell

input buff_1 abc satya_1 pvr_1 buff_2 def satya_1 pvr_1 buff_3 ghi satya_1 pvr_1 buff_4 jkl satya_1 pvr_1 required out put buff_1 abc satya_1 pvr_1 abc satya_1 buff_2 def satya_1 pvr_1 def satya_1 buff_3 ghi satya_1 pvr_1 ghi satya_1 (6 Replies)
Discussion started by: pvr_satya
6 Replies

7. Shell Programming and Scripting

sed or awk scripting help needed

hi all, for an example : df -k output shows: $ df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p6 3099260 1117760 1824068 8% / /dev/cciss/c0d0p1 256666 18065 225349 8% /boot none 8219180 0 8219180 0% /dev/shm /dev/mapper/vglocal-home 1032088 245172 734488 26%... (7 Replies)
Discussion started by: raghur77
7 Replies

8. Shell Programming and Scripting

usage...sed/awk/reg-exp ..in shell scripting

in shell scripting there is extensive usage of i> regular expression ii>sed iii>awk can anyone tell me the suitable contexts ...i mean which one is suitable for what kind of operation. like the reg-exp and sed seems to be doing the same job..i.e pattern matching (1 Reply)
Discussion started by: mobydick
1 Replies

9. Shell Programming and Scripting

scripting with awk and sed

hey all, i was just wondering if it was possible to to get data from user input , and parse it through sed to remove or add what that user has entered into a flat file? do i need awk ? any help is greatly appreciated ~shan2on (2 Replies)
Discussion started by: shan2on
2 Replies

10. Shell Programming and Scripting

Help on SED AWK in shell scripting

Hi, I have a script abc.sql which contains a word 'timestamp'. I have another script xyz.txt genrated everyweek, which has a new timestamp value every week. How do I replace the word 'timestamp' in script abc.sql with the value mentioned in the script xyz.txt, so that I can run the script... (3 Replies)
Discussion started by: kaushys
3 Replies
Login or Register to Ask a Question