Using sed to change file into an awk script

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using sed to change file into an awk script
# 1  
Old 08-18-2019
Using sed to change file into an awk script

Hi I want to use sed to change a text files input into an awk script.
For example if the input says " chord -- english " I want to change this using sed 's/pattern 1 /pattern 2 /'g filename but I don't understand how to use part of the pattern 1 to input that into pattern 2 . Like after using sed chord-- english should look like this awk '/pattern 1/ {awk function}' filename.

Any help will be appreciated.

Last edited by jim mcnamara; 08-18-2019 at 10:53 AM..
# 2  
Old 08-18-2019
Not sure I understand the request. You have a text file processed by awk and want it preprocessed by sed? Why don't you do all of it in awk itself?
# 3  
Old 08-18-2019
Do you need distinct awk functions for every line of text or just a few functions? Sample input, output would help a lot.

Edit: I don't understand the request either....
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 08-18-2019
Actually, my main issue is that I want to use part of the pattern 1 in pattern 2 in sed. Like so

-sagot this should change to /sagot$/{count++}
-sagot- this should change to /sagot/{count++}
sagot- should change to /^sagot/{count++}

so basically I need to change according to the position of '-' and use the word in pattern 1 without hard coding it treating as if it is a variable because I have to run this on a big file and cannot write all the combinations.
# 5  
Old 08-18-2019
That's what "back references" (man regex / man sed) are for. But I think your "ansatz" is flawed - you could use the "pattern1" in awk only with tricks.
Paint the whole picture, include sample input and output data, and you might receive proposals to solve the whole picture.
# 6  
Old 08-18-2019
I have to use sed to transform every line into an awk code

Last edited by enforcer; 08-20-2019 at 12:10 AM..
# 7  
Old 08-18-2019
Hmm. Assuming the file looks like your example -
FWIW you can do this many ways, you may be over thinking your task.

greek=$(grep -c "[Gg]reek$" infile)
latin=$(grep -c "[Ll]atin$" infile)
echo "greek= $greek   latin= $latin"

This provides the same information your awk preprocessing generates, with no intermediate files. And ignores upper/lowercase issues.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Datestamp format 2nd change in csv file (awk or sed)

I have a csv file formatted like this: 2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07and I'm trying to change it to MM/DD/YYYY HH:MM for both occurances. I have got this: awk -F, 'NR <=1 {print;next}{"date +%d/%m/%Y\" \"%H:%m -d\""$1 "\""| getline dte;$1=dte}1' OFS="," test.csvThis... (6 Replies)
Discussion started by: say170
6 Replies

2. 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

3. Shell Programming and Scripting

sed substitution or awk, need to direct change the file

I want change the file when the line contains $(AA) but NOT contains $(BB), then change $(AA) to $(AA) $(BB) eg: $(AA) something $(AA) $(BB) something (7 Replies)
Discussion started by: yanglei_fage
7 Replies

4. Shell Programming and Scripting

Creating a sed script to change ip addresses in a file

So I'm new to this sed command and I am trying to create a script that replaces ip addresses when I name a file but can't tweak it to work. Here is what it looks like: #!/bin/bash # file=$1 # sed -e 's/-CPUaddr' -i "$file" sed -e 's/-CPUaddr... (10 Replies)
Discussion started by: uradunce
10 Replies

5. Shell Programming and Scripting

Need help with a script to process a CSV file using SED and AWK

I get a CSV file every day with 2 columns and multiple rows ex: date1,date2 ( both the fields are varchar fields) This data has to be updated in a table which is being done manually and i want to automate that. 1. I have to select all the data from the prod table( 2 columns { date1,date2}) into... (4 Replies)
Discussion started by: kkb
4 Replies

6. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies

7. Linux

SED/AWK Script to clear log file using timestamp?

I have a log file on our system which fills up with lines that have been timestamped, as follows.... 03/03/2008 10:56:06:815] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:06:820] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:07:003]... (2 Replies)
Discussion started by: davesimm
2 Replies

8. Shell Programming and Scripting

awk/sed/ksh script to cleanup /etc/group file

Many of my servers' /etc/group file have many userid's that does not exist in /etc/passwd file and they need to be deleted. This happened due to manual manipulation of /etc/passwd files. I need to do this for 40 servers. Can anyone help me in achieving this? Even reducing a step or two will be... (6 Replies)
Discussion started by: pdtak
6 Replies

9. Shell Programming and Scripting

How to change ip using awk or sed .

How to change ip using awk or sed . #cat /etc/hosts server1 server2 server3 server4 server5 Output: server1 server2 server3 server4 server5 (9 Replies)
Discussion started by: kenshinhimura
9 Replies

10. Shell Programming and Scripting

filter parts of a big file using awk or sed script

I need an assistance in file generation using awk, sed or anything... I have a big file that i need to filter desired parts only. The objective is to select (and print) the report # having the string "apple" on 2 consecutive lines in every report. Please note that the "apple" line has a HEX... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question

Featured Tech Videos