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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to add prefix using sed or awk from cat the file
# 8  
Old 10-28-2017
OK, still not clear to me. But with the second part of post #5, let's try this:
Code:
awk '
  NR==FNR {
    A[$1]
    next
  }
  { 
    if($2 in A)
      $3=$3 "_found"
    else
      $3=$3 "_not_found"
    print
  }
' file2 FS=, OFS=, file1

Output:
Code:
Server1,dev14,nofast_found,TDEV RW 69053
Server2,dev4,nofast_not_found,TDEV RW 69053
Server2,dev1,nofast_found,TDEV RW 103579
Server3,dev7,nofast_not_found,TDEV RW 69053


Last edited by Scrutinizer; 10-28-2017 at 03:38 AM..
# 9  
Old 10-30-2017
Sorry the input file has changed. I need to grep first only the device which has "no_fast" and check with file-2 if the devs found update the row with fast_found and other no_fast device to no_fast_no_found.

And i need to output with all the row in same order as inputfile

File-1 (Updated new input)
Code:
frame1,no_fast,Server1, 9858 N/A TDEV RW 34526 
frame1,no_fast,Server1, 9859 N/A TDEV RW 34526 
frame1,no_fast,Server1, 985A N/A TDEV RW 34526 
frame1,no_fast,Server1, 985C N/A TDEV RW 103579 
frame1,no_fast,server2, 3F9C N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3F9D N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3F9E N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3F9F N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3FA0 N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3FA1 N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3FA2 N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3FA3 N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3FA4 N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3FA5 N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 3FA6 N/A RDF1+TDEV RW 34526 
frame1,no_fast,server2, 6204 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA7 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA8 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA9 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FAA N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FAB N/A RDF1+TDEV RW 34526

file-2
Code:
3F9E
9858

Output Required
Code:
frame1,fast_found,Server1, 9858 N/A TDEV RW 34526 
frame1,no_fast_not_found,Server1, 9859 N/A TDEV RW 34526 
frame1,no_fast_not_found,Server1, 985A N/A TDEV RW 34526 
frame1,no_fast_not_found,Server1, 985C N/A TDEV RW 103579 
frame1,no_fast_not_found,server2, 3F9C N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3F9D N/A RDF1+TDEV RW 34526 
frame1,fast_found,server2, 3F9E N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3F9F N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA0 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA1 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA2 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA3 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA4 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA5 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA6 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 6204 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA7 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA8 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA9 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FAA N/A RDF1+TDEV RW 34526
frame1,VMAX_CLASS2,server3, 3FAB N/A RDF1+TDEV RW 34526


Moderator's Comments:
Mod Comment Please use CODE (not QUOTE) tags correctly for data as well as required by forum rules!

Last edited by RudiC; 10-30-2017 at 09:47 AM.. Reason: Changed CODE tags.
# 10  
Old 10-30-2017
Hello ranjancom2000,

Please use code tags as per forum rules for sample Input_file and expected output. Could you please let me know if this helps you.
Code:
awk '
FNR==NR{
  a[$0];
  next
}
{
num=split($1, array,",")
}
{
array[2]=$2 in a?"fast_found":"no_fast_no_found";
for(i=1;i<=num;i++){
  val=val?val","array[i]:array[i]
};
$1=val
}
{
print;
val=""
}' File-2 File-1

Output will be as follows.
Code:
frame1,fast_found,Server1, 9858 N/A TDEV RW 34526
frame1,no_fast_no_found,Server1, 9859 N/A TDEV RW 34526
frame1,no_fast_no_found,Server1, 985A N/A TDEV RW 34526
frame1,no_fast_no_found,Server1, 985C N/A TDEV RW 103579
frame1,no_fast_no_found,server2, 3F9C N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3F9D N/A RDF1+TDEV RW 34526
frame1,fast_found,server2, 3F9E N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3F9F N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3FA0 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3FA1 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3FA2 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3FA3 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3FA4 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3FA5 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 3FA6 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server2, 6204 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server3, 3FA7 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server3, 3FA8 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server3, 3FA9 N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server3, 3FAA N/A RDF1+TDEV RW 34526
frame1,no_fast_no_found,server3, 3FAB N/A RDF1+TDEV RW 34526

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 11  
Old 10-30-2017
Hi RavinderSingh13,

Script is working but it print in the screen i need redirect to file. Where i need to put the redirect to file output
# 12  
Old 10-30-2017
This is the third time you change your request. How about getting your act together in the first place, and post a decent, meaningful, and carefully phrased specification that you don't need to correct several times?

For your last request, try
Code:
awk -F, '
FNR==NR         {T[$0]
                 next
                }

/no_fast/       {num = split ($NF, ARR, " ")
                 if (ARR[1] in T)       sub (/no_/, "", $2)
                   else                 sub (/fast/, "&_not", $2)
                 $2 = $2 "_found"
                }
1
' OFS=","  file2 file1
frame1,fast_found,Server1, 9858 N/A TDEV RW 34526 
frame1,no_fast_not_found,Server1, 9859 N/A TDEV RW 34526 
frame1,no_fast_not_found,Server1, 985A N/A TDEV RW 34526 
frame1,no_fast_not_found,Server1, 985C N/A TDEV RW 103579 
frame1,no_fast_not_found,server2, 3F9C N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3F9D N/A RDF1+TDEV RW 34526 
frame1,fast_found,server2, 3F9E N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3F9F N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA0 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA1 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA2 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA3 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA4 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA5 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 3FA6 N/A RDF1+TDEV RW 34526 
frame1,no_fast_not_found,server2, 6204 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA7 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA8 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FA9 N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FAA N/A RDF1+TDEV RW 34526 
frame1,VMAX_CLASS2,server3, 3FAB N/A RDF1+TDEV RW 34526

# 13  
Old 10-30-2017
Quote:
Originally Posted by ranjancom2000
Hi RavinderSingh13,
Script is working but it print in the screen i need redirect to file. Where i need to put the redirect to file output
Hello ranjancom2000,

You need to put > Output_file at the end of above code, let me know if any queries on same.

Thanks,
R. Singh
# 14  
Old 10-30-2017
Hi RudiC,

Sorry the output file keep changing every time. So i need to request but i can get the RavinderSingh13 script working. Now i need the print out to file not on screen.

---------- Post updated at 11:51 AM ---------- Previous update was at 08:14 AM ----------

I am using code tag for this. I think this is correct

Hi RavinderSingh13,

Some strange i ran the script by using code. But i this output it was renaming the "Tier1" also. Could you please chk what is causing this issue


Input file
Code:
Frame1,Tier1,Server1, 120B N/A TDEV RW 34526 
Frame1,Tier1,Server1, 120C N/A TDEV RW 34526 
Frame1,Tier1,Server1, 120D N/A TDEV RW 34526 
Frame1,Tier1,Server1, 120E N/A TDEV RW 34526 
Frame1,Tier1,Server1, 120F N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1210 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1211 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1212 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1213 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1214 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1215 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1216 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1217 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1218 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 1219 N/A TDEV RW 34526 
Frame1,Tier1,Server1, 121A N/A TDEV RW 34526 
Frame1,Tier1,Server1, 121B N/A TDEV RW 34526 
Frame1,Tier1,Server1, 121C N/A TDEV RW 34526 
Frame1,Tier1,Server1, 121D N/A TDEV RW 34526 
Frame1,Tier1,Server1, 121E N/A TDEV RW 34526 
Frame1,Tier1,Server1, 121F N/A TDEV RW 34526 
Frame1,no_fast,Server2, 2955 N/A RDF1+TDEV RW 69053 
Frame1,no_fast,Server2, 2995 N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 2999 N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 299D N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 29A1 N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 29A5 N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 29A9 N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 29AD N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 29B1 N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 29B5 N/A RDF1+TDEV RW 138105 
Frame1,no_fast,Server2, 2A73 N/A RDF1+TDEV RW 34526

output file
Code:
Frame1,no_fast_Not_Bounded,Server1, 120B N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 120C N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 120D N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 120E N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 120F N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 1210 N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 1211 N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 1212 N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 1213 N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 1214 N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 1215 N/A TDEV RW 34526
Frame1,no_fast_Not_Bounded,Server1, 1217 N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 1218 N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 1219 N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 121A N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 121B N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 121C N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 121D N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 121E N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server1, 121F N/A TDEV RW 34526 
Frame1,no_fast_Not_Bounded,Server2, 2955 N/A RDF1+TDEV RW 69053 
Frame1,no_fast_Not_Bounded,Server2, 2995 N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 2999 N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 299D N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 29A1 N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 29A5 N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 29A9 N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 29AD N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 29B1 N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 29B5 N/A RDF1+TDEV RW 138105 
Frame1,no_fast_Not_Bounded,Server2, 2A73 N/A RDF1+TDEV RW 3452

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add the word "prefix" to beginning of line using sed

SUSE linux bash shell this works test -d /tmpp && echo "directory exists" || echo "directory doesn't exists" |sed -e "s/^/prefix /" prefix directory doesn't exists but why doesn't this work? test -d /tmp && echo "directory exists" || echo "directory doesn't exists" |sed -e... (3 Replies)
Discussion started by: snoman1
3 Replies

2. Shell Programming and Scripting

awk move select fields to match file prefix in two directories

In the awk below I am trying to use the file1 as a match to file2. In file2 the contents of $5,&6,and $7 (always tab-delimited) and are copied to the output under the header Quality metrics. The below executes but the output is empty. I have added comments to help and show my thinking. Thank you... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

4. UNIX for Dummies Questions & Answers

How to pass cat file in awk statement?

Hi, I am working on kernel parameters, want to check values under /proc/sys/kernel below I tried for kernel.sem SEMMNS: 4096 cat /proc/sys/kernel/sem | awk '{print $2}' awk '{ if ($2 < 33000) print }' /proc/sys/kernel/sem |awk '{print $2}' 32000 The above... (7 Replies)
Discussion started by: stew
7 Replies

5. Linux

Adding a prefix to a column using awk/sed commands

Hello, I am a newbie to linux and struggling to find a better way to append a column in a text file. Here is the file i want to modify: It has 8 columns (and thousands of rows). I want to append the first column by adding "chr" infront of the numbers. Some rows have a string in the first... (4 Replies)
Discussion started by: bjorngill
4 Replies

6. Shell Programming and Scripting

awk or sed to add field in a text file

Hi there, I have a csv file with some columns comma sepated like this : 4502-17,PETER,ITA2,LEGUE,92,ME - HALF,23/05/10 15:00 4502-18,CARL,ITA2,LEGUE,96,ME - HALF,20/01/09 14:00 4502-19,OTTO,ITA2,LEGUE,97,ME - MARY,23/05/10 15:00 As you can see the column n. 7 is a timestamp column, I need... (23 Replies)
Discussion started by: capnino
23 Replies

7. Shell Programming and Scripting

Need awk script to add a prefix to each line in file

Hello , I have file with below content : '165567885', '165568443', '165568805', I need an awk script that would add a prefix zero after first ' . Like '0165567885', '0165568443', '0165568805', Please help. Thanks in advance. (5 Replies)
Discussion started by: rmv
5 Replies

8. Shell Programming and Scripting

Remove prefix using awk

Remove prefix using awk File: nawk -F"|" '{if ($1 ~ /^xyz./) print; else { gsub(.*\..*, \..*, $1) ;print }}' file Error: ouput required: (5 Replies)
Discussion started by: pinnacle
5 Replies

9. UNIX for Dummies Questions & Answers

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... (6 Replies)
Discussion started by: MrKlint
6 Replies

10. Shell Programming and Scripting

Problem to add the string(without sed & awk) into the middle of file

Hi, I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution. I first tried by $echo "paki" >> file This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or... (5 Replies)
Discussion started by: ali hussain
5 Replies
Login or Register to Ask a Question