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
# 1  
Old 10-27-2017
Need to add prefix using sed or awk from cat the file

I need the use sed or AWK using cat the file

Code:
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
RDF1+TDEV RW 34526
RDF1+TDEV RW 34526
TDEV RW 34526

I need the output like this

Code:
Node1,TDEV RW 1035788
Node1,TDEV RW 1035788
Server1,TDEV RW 69053
Server2,TDEV RW 69053
Server2,TDEV RW 103579
Server3,TDEV RW 69053
server4,RDF1+TDEV RW 69053
server4,RDF1+TDEV RW 517894
server4,RDF1+TDEV RW 621473
server6,TDEV RW 34526
server6,TDEV RW 34526
server6,RDF1+TDEV RW 34526
server6,RDF1+TDEV RW 34526
server6,TDEV RW 34526

# 2  
Old 10-27-2017
This problem has been solved umpteen times in these fora. Did you bother to do a search as recommended in the rules?

However, try
Code:
awk 'NF == 1 {PRFX = $1; next} {print PRFX "," $0}' file
Node1,TDEV RW 1035788
Node1,TDEV RW 1035788
Server1,TDEV RW 69053
Server2,TDEV RW 69053
Server2,TDEV RW 103579
Server3,TDEV RW 69053
server4,RDF1+TDEV RW 69053
server4,RDF1+TDEV RW 517894
server4,RDF1+TDEV RW 621473
server6,TDEV RW 34526
server6,TDEV RW 34526
server6,RDF1+TDEV RW 34526
server6,RDF1+TDEV RW 34526
server6,TDEV RW 34526


Last edited by RudiC; 10-30-2017 at 10:04 AM.. Reason: Typo
# 3  
Old 10-27-2017
Thanks i am search in google not able to find the right code. It work great if i have any doubt on procession i will come back

---------- Post updated at 08:31 AM ---------- Previous update was at 07:17 AM ----------

Hi RudiC

Could you please help to provide a solution using awk or diff command. I need to add one more prefix by compare a file input. Since the file very large it will contain more then 10k devs. I can't compare in loop. Instead in want to use diff or some other compare command

Real file
Code:
Node1,dev2,nofast,TDEV RW 1035788
Node1,dev10,nofast,TDEV RW 1035788
Server1,dev14,nofast,TDEV RW 69053
Server2,dev4,nofast,TDEV RW 69053
Server2,dev1,nofast,TDEV RW 103579
Server3,dev7,nofast,TDEV RW 69053
server4,dev11    RDF1+TDEV RW 69053
server4,dev9    RDF1+TDEV RW 517894
server4,dev3    RDF1+TDEV RW 621473
server6,dev5,nofast,TDEV RW 34526
server6,dev8,nofast,TDEV RW 34526
server6,dev13    RDF1+TDEV RW 34526
server6,dev12    RDF1+TDEV RW 34526
server6,dev6,nofast,TDEV RW 34526

Code:
The input file will have 20k devs like that 

dev14
dev15
dev16
dev17
dev18
dev19
dev20
dev21
dev1
dev23
dev24
dev25

I need output will be like this "nofast,T1"

Code:
Server1,dev14,nofast,T1,TDEV RW 69053
Server2,dev4,nofast,TDEV RW 69053
Server2,dev1,nofast,T1,TDEV RW 103579
Server3,dev7,nofast,TDEV RW 69053
server4,dev11    RDF1+TDEV RW 69053
server4,dev9    RDF1+TDEV RW 517894
server4,dev3    RDF1+TDEV RW 621473
server6,dev5,nofast,TDEV RW 34526
server6,dev8,nofast,TDEV RW 34526
server6,dev13    RDF1+TDEV RW 34526
server6,dev12    RDF1+TDEV RW 34526
server6,dev6,nofast,TDEV RW 34526

# 4  
Old 10-27-2017
Not clear. How do you connect Node1 to dev2 and dev10, and server1 to dev14? Where does nofast and / or T1 come from? Why and when do they show up together and when not?
Please be VERY specific in your answer!
# 5  
Old 10-27-2017
Quote:
Originally Posted by RudiC
Not clear. How do you connect Node1 to dev2 and dev10, and server1 to dev14? Where does nofast and / or T1 come from? Why and when do they show up together and when not?
Please be VERY specific in your answer!
That was final processed output after this output i need to compare with the file which will have only devs info. I need check the file and compare only with the device mention in "nofast". I dont want to use "forloop" since number of device will be more it will take time. Instead i want to compare only the awk output of nofast device and update the prefix

If the device found in the input file update the field with nofast_T1 if not found update with nofast_nofound

---------- Post updated at 09:40 AM ---------- Previous update was at 08:48 AM ----------

Source file -file1
Code:
Server1,dev14,nofast,TDEV RW 69053
Server2,dev4,nofast,TDEV RW 69053
Server2,dev1,nofast,TDEV RW 103579
Server3,dev7,nofast,TDEV RW 69053

Input file -file2
Code:
dev14
dev15
dev16
dev17
dev18
dev19
dev20
dev21
dev1
dev23
dev24
dev25

I need 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


I need to use awk lookup on this. But i dont how to filter source file coloum2 and Vlookup with input file and check the record is found on that file

Code:
awk 'NR==FNR {m[$1]=$1; next} {$1=m[$1]; print}' file1 file2

This User Gave Thanks to ranjancom2000 For This Post:
# 6  
Old 10-27-2017
Quote:
Originally Posted by ranjancom2000
That was final processed output after this output i need to compare with the file which will have only devs info. I need check the file and compare only with the device mention in "nofast". I dont want to use "forloop" since number of device will be more it will take time. Instead i want to compare only the awk output of nofast device and update the prefix

If the device found in the input file update the field with nofast_T1 if not found update with nofast_nofound

---------- Post updated at 09:40 AM ---------- Previous update was at 08:48 AM ----------

Source file -file1
Code:
Server1,dev14,nofast,TDEV RW 69053
Server2,dev4,nofast,TDEV RW 69053
Server2,dev1,nofast,TDEV RW 103579
Server3,dev7,nofast,TDEV RW 69053

Input file -file2
Code:
dev14
dev15
dev16
dev17
dev18
dev19
dev20
dev21
dev1
dev23
dev24
dev25

I need 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


I need to use awk lookup on this. But i dont how to filter source file coloum2 and Vlookup with input file and check the record is found on that file

Code:
awk 'NR==FNR {m[$1]=$1; next} {$1=m[$1]; print}' file1 file2

Please reread your statement of what needs to be done marked in red above. Then note that some lines in your output have nofast_found which is not mentioned as an output that should be produced. And note that no output contains the string nofast_T1 which you said should be produced for any line where the 2nd field in file1 is found as a 1st field value in file2. And note that no output contains the string nofast_nofound (although some do contain nofast_not_found).

If you can't give us a description of the work that needs to be done by a script that matches the output you say you want, there isn't much chance of us being able to help you write a script that does what you want it to do.

Is there still some other intermediate step that you haven't told us about that changes nofast_T1 to nofast_found and changes nofast_nofound to nofast_not_found possibly with some other requirements you haven't mentioned?
# 7  
Old 10-28-2017
Hi DOn,

Pls use my input as new program i need to compare two file and provide new output

First file will has the information has mention (file1)
Second file will be (file2)

I need to Vlookup each line from file1 "column 2" to each line of "File2"

Update the "File1" for all the "dev" found from "file2" has

dev14,Found
dev4,not_found

I cant use forloop since i have to check more then 10k devs from file1 with file2. Instead is there any solution what we do in excel Vlookup
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