The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Scripting help tiney83 UNIX for Dummies Questions & Answers 4 05-14-2008 09:04 PM
i m new to scripting ip.bastola Shell Programming and Scripting 1 03-14-2007 01:09 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 09:12 AM
scripting guru's pls help me with scripting on AIX thatiprashant Shell Programming and Scripting 1 01-20-2006 07:58 PM
Help!? with scripting... BlueOysterCult UNIX for Dummies Questions & Answers 2 12-09-2003 06:23 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-26-2009
MrKlint MrKlint is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
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 (permalink)  
Old 01-26-2009
slant-40 slant-40 is offline
Registered User
  
 

Join Date: Apr 2007
Location: West Babylon NY 11704
Posts: 10
Try this, see if it works:

for file in *; do
(do your translation here) < $file > temp.file
mv temp.file $file
done
  #3 (permalink)  
Old 01-26-2009
MrKlint MrKlint is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
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 (permalink)  
Old 01-26-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool

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 (permalink)  
Old 01-26-2009
MrKlint MrKlint is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
I guess this is scripting. I have never done that before, but I will try to find out how to do it.

Thanks.
  #6 (permalink)  
Old 01-26-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool

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 (permalink)  
Old 01-30-2009
MrKlint MrKlint is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
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
Closed Thread

Bookmarks

Tags
awk, cat, script, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0