Visit Our UNIX and Linux User Community


using redirecting from awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using redirecting from awk
# 1  
Old 03-21-2006
using redirecting from awk

Well this is what im doing, im writing a script that you pass 3 variables into. Filename, delimiter or "FS in AWK", and a string of columbs you want to keep 1,2,4,5... Just modifing a data file and rewriting with a different extension.

My problem atm is using awk to seperate the "columb String"
**Input runme.sh filename.txt "|" 1,2,4,5**

echo $3 | awk -F"," ' {for (i=1;i <=NF;i++) print $i }' |**well i want this redirected into an array..
**Output from this command gives me 1 2 4 5

I need to rebuild this into another awk command."like this"
to get my needed data
needs to be
awk -F"$dell" -v dell=$dell 'BEGIN{OFS= dell }{print $2,$4,""} <--dynamic
END{Print"for Kicks"}' $filename > newoutputfile.txt
**********************************************
OK found a solution myself this is in KSH

echo $time | awk -F"," '{ORS=" " }{OFS=""}{for(i=1;i <=NF;i++) print "$",$i }' | read ouput

set -A array $output

echo ${array[0]}
echo ${array[1]}
echo ${array[3]}

Last edited by ugh; 03-22-2006 at 11:18 AM.. Reason: Found the solution to the problem
# 2  
Old 03-22-2006
something to start with - not tested:
Code:
#!/bin/ksh

filename='myFile'
newEXT='.new'
columns2keep='1 2 4 5'
del=','

nawk -F"${del}" -v cols="${columns2keep}" '
   BEGIN {
      OFS=FS
      n=split(cols, colsA, " ")
   }
   {
       for(i=1; i <= n; i++)
           printf("%s%s", $colsA[i], (i<n) ? OFS : "\n")
    }
' "${filename}" > "${filename}${newEXT}"

# 3  
Old 03-27-2006
Thanks

Thanks Vgersh99 that looks close to what i need, though i cant use nawk and i dont think i can manipulate the output string the same way in awk. I ended up using a different method, crude but it worked.

filename=$1
dell=$2
keep=$3

echo $keep | awk -F"," '{ORS=" " }{OFS=""}{gtw=","} {for(i=1;i <=NF;i++){ print "$",$i,gtw; if((i+1) == NF) gtw="" } }' | read output

i="awk -F\"$dell\" 'BEGIN{OFS=FS}{print $output}' $filename"

echo $i > temp.sh
chmod 700 temp.sh
temp.sh

Previous Thread | Next Thread
Test Your Knowledge in Computers #1011
Difficulty: Easy
The number 128 in base 10 equals 10000000 in base 2.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting output using if with awk

I have this line were I am selecting some fields from one file and creating a new file for the selected data. awk -F "\"*,\"*" '{print $1"," $2}' folderone/list.txt > folderone/qlist.txt This works, but then from this new file I want it to create a new file where it separates data: $2 >5 it... (2 Replies)
Discussion started by: rluna
2 Replies

2. Shell Programming and Scripting

Awk: Print Error While Redirecting output in multiple Files

Hi, I have a following code in which I am unable to redirect to multiple files. Can anybody please help with some corrections awk -F, '{ if ( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST") { array1++ array2++ array3++ } else if (substr($1,26,2)=="03" &&... (4 Replies)
Discussion started by: siramitsharma
4 Replies

3. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

4. Shell Programming and Scripting

awk script - redirecting out put based on mapping

Need awk solution. Please advise. inputfile.txt 1,NY, 1111 2,MI, 222 3,NY,333 4,OH,444 5,OH,555 mapping.txt NY NYNY IL ILLINOIS OH OHIO Need to write a code which will compare 2nd column of inputfile.txt with mapping file and redirect output based on the... (2 Replies)
Discussion started by: vegasluxor
2 Replies

5. Shell Programming and Scripting

redirecting output using if-then-else in awk

I am trying to filter records based on number of "|", delimiter in my application. If number of "|" is greater than 14 its a bad record, else its a good record. I have to redirect output to two different files based on the if-then-else evaluation in AWK. if number of | in file_0 > 14 ... (2 Replies)
Discussion started by: siteregsam
2 Replies

6. Shell Programming and Scripting

redirecting help

I am trying to create the file and redirect the output in the same command line which is line 4 in the below program. #!/bin/bash read -p "Enter File Name:" value1 echo "Your File Name is $value1" sed 's/abcd/'$value1'/g' abcd_calls > $value1_calls This is the error it generates ... (3 Replies)
Discussion started by: learnbash
3 Replies

7. Shell Programming and Scripting

Redirecting to different output files with awk.

Well, it didn't take me long to get stumped again. I assure you that I'm not mentally deficient, just new to scripting. So, here's the gist. I want to redirect output from awk based off of which branch of an if-else statement under which it falls. #!/bin/bash #some variables... (2 Replies)
Discussion started by: mikesimone
2 Replies

8. UNIX for Dummies Questions & Answers

awk and redirecting to files

Hello, I have a set of data (comma seperated) that I want to save to multiple files according to one of the fields in the data set. I can easily do this with the following script: BEGIN { FS = OFS = ","} NF {print $0 >> ($2 "" ".csv")} It works perfectly on a set of dummy data I have set... (8 Replies)
Discussion started by: pfft
8 Replies

9. Shell Programming and Scripting

Confused about redirecting output from awk.

I have a simple script written in awk whose purpose is to go through some php files and replace some strings. Naturally, I want the changes to be written out to the files. The script looks generally as follows: { gsub(/'replacethis'/, "with this"); # a bunch of these print $0 > FILENAME }... (3 Replies)
Discussion started by: face1
3 Replies

10. UNIX for Advanced & Expert Users

MX redirecting

I in no means consider myself a expert in unix however my question is a little more complex. I am running a mac os 10 server and all web settings and email settings have been set up using the unix terminal. I am in the process or redirecting my mxrecords to a spam filtering company but i am... (11 Replies)
Discussion started by: nbredthauer
11 Replies

Featured Tech Videos