Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Awk/sed to replace variable in file Post 303041166 by reldb on Saturday 16th of November 2019 02:18:07 AM
Old 11-16-2019
Awk/sed to replace variable in file

Hi All

I have one file with multiple lines in it, each line has static text and some variable enclosed in <<filename>> as well. e.g. as below
Code:
123, <<file1.txt>> this is my name, I stay at <<city.txt>> Thanks for visiting
348384y,  this is my name <<fileabc.txt>>, I stay at near the mall of <<cityxyz.txt>> welcome

each variable is a file which has content as below
Code:
cat file1.txt
scott
thomas

Code:
cat city.txt
CHICAGO 
LA
TAT

I am looking for a code which can print the file line with variable value from each file as Cartesian Product as below
Code:
123, scott this is my name, I stay at CHICAGO Thanks for visiting
123, scott this is my name, I stay at LA Thanks for visiting
123, scott this is my name, I stay at TAT Thanks for visiting
123, thomas this is my name, I stay at CHICAGO Thanks for visiting
123, thomas this is my name, I stay at LA Thanks for visiting
123, thomas this is my name, I stay at TAT Thanks for visiting
similar way second line with value from their file name per variable

Is there any quicker/simpler way to do in awk/sed/perl or shell scripting?

Thanks
rel

Last edited by RavinderSingh13; 11-16-2019 at 11:55 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace with variable using sed or awk

hi, i have file say email.temp looks like Bell_BB 17 Bell_MONTHLY 888 SOLO_UNBEATABLE 721 and another file r3 Bell BB,Bell_BB Bell,Bell_MONTHLY SOLO,SOLO_UNBEATABLE i want email.temp files $1 say Bell_BB should be replaced by r3 Bell BB and Bell_MONTHLY by... (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

2. Shell Programming and Scripting

using file-and-replace sed command with the use of a variable?

Ok, so, let's say I have the variable $GMAILID....How can I use it with sed command so to replace a string in a file? e.g.: sed -i 's/$GMAILID/test@gmail.com/' /home/$USER/Desktop/sendmail (4 Replies)
Discussion started by: hakermania
4 Replies

3. Shell Programming and Scripting

Sed or awk for batch replace file name

Can you please point me in the correct direction? I need a line or script to run though a given directory and find all files with "@domain.local" in there names and simple remove that. For example if the files were named 1234@domain.local the file would then become 1234. (1 Reply)
Discussion started by: binary-ninja
1 Replies

4. Shell Programming and Scripting

using sed/awk to replace a block of text in a file?

My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Discussion started by: kiddsupreme
12 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

7. Shell Programming and Scripting

Replace value of a variable in a file through script using sed

Hi, I am trying to replace the value of a variable in a file through another script. Example: Filename : abc.txt contents: a=10 b=20 c=30 Need to change the value of, say, b - Tried using the following: sed "s/${b}/15/g" abc.txt Have tried various forms of sed (with single quotes,... (4 Replies)
Discussion started by: rituparna_gupta
4 Replies

8. Shell Programming and Scripting

sed/awk script to replace only FIRST comment in the file

My first comment on every file contains the license message. I want to replace with a new license message. I used the below sed script, which replaces all comments. What is the modification or any other method with awk script for the below to edit only the first comment(license message)? #sed -f... (1 Reply)
Discussion started by: vpshastry
1 Replies

9. Shell Programming and Scripting

How to replace a string with a variable in a file using sed?

I have a file having some text like: PATH_ABC=/user/myLocation I have to replace "/user/myLocation" with a session variable say, $REPLACE_PATH, where $REPLACE_PATH=/user/myReplaceLocation The following sed command is not working. It is writing PATH_ABC=$REPLACE_PATH in the file ... (2 Replies)
Discussion started by: SKhan
2 Replies

10. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies
PMSPECLOCALPMDA(3)					     Library Functions Manual						PMSPECLOCALPMDA(3)

NAME
__pmSpecLocalPMDA - process command-line argument for the table of DSO PMDAs C SYNOPSIS
#include <pcp/pmapi.h> #include <pcp/impl.h> char *__pmSpecLocalPMDA(const char *spec); cc ... -lpcp DESCRIPTION
PCP contexts of type PM_CONTEXT_LOCAL are used by clients that wish to fetch metrics directly from one or more PMDAs on the local host without involving pmcd(1). __pmSpecLocalPMDA provides a convenience wrapper to be used by applications that wish to use a command line argument (usually with -K) to control the DSO PMDAs that are available for a PM_CONTEXT_LOCAL context. The spec argument specifies actions for one or more DSO PMDAs using up to four fields separated by commas (``,''), namely: - an opcode with one of the values add (add a new entry), del (delete an existing entry) or clear (clear all entries from the table). - the PMDA's domain number - the path to the PMDA DSO (may be absolute or relative to the $PCP_VAR_DIR/pmdas directory and the DSO suffix is optional), and - the name of the PMDA's initialization routine. All fields are required to add a new entry. To delete an entry the opcode is required plus either or both of the domain number and path fields. To clear all entries, only the opcode is required. If spec is parsed successfully, then __pmLocalPMDA(3) is called with the extracted arguments. RETURN VALUE
On success, __pmSpecLocalPMDA will return NULL. On error or failure, __pmSpecLocalPMDA will return a pointer to a static error message. EXAMPLES
Some examples of valid spec strings: clear Delete all entries from the DSO table. add,123,foo/foo_pmda,foo_init Add the ``foo'' PMDA using domain 123. The PMDA's DSO is most likely in below the directory $PCP_PMDAS_DIR and named foo/foo_pmda.so (for ELF-style platforms) or foo/foo_pmda.dylib (for BSD-style platforms) or fooo_pmda.dll (for Windows-style plat- forms). The initialization routine for the ``foo'' PMDA is foo_init(). del,123 Delete the entry for the DSO with domain 123. del,,foo/foo_pmda Delete the entry with a pathname to the DSO that matches foo/foo_pmda. del,123,foo/foo_pmda Delete the entry for the DSO with either domain 123 and/or a pathname to the DSO that matches foo/foo_pmda. SEE ALSO
PMAPI(3), __pmLocalPMDA(3) and pmNewContext(3). Performance Co-Pilot PMSPECLOCALPMDA(3)
All times are GMT -4. The time now is 01:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy