4
votes
2answers
52 views

How to exchange words in a filename using the shell?

One of my customers is a school. On their file server all projects have a directory with the same naming scheme. <last name>, <first name> <second name>? - <project ...
2
votes
3answers
97 views

Call sed to replace a string stored in a bash variable

I want to expand a variable in bash with sed or awk. This variable is an array. For example, the script must delete strings contained in array. I tried ARRAY1=( string1 string2 string3 ) sed -i ...
0
votes
1answer
28 views

Replace copyrights recursively [duplicate]

I have many files under the current directory. Each file contains copyrights headers like, # # Copyright 2013 Company, Inc. All rights reserved. This software # is property of Company, ...
5
votes
4answers
220 views

Is there a difference between read, head -1, and sed 1q?

The following commands seem to be roughly equivalent: read varname varname=$(head -1) varname=$(sed 1q) One difference is that read is a shell builtin while head and sed aren't. Besides that, is ...
0
votes
2answers
36 views

Add space after 8 characters then add space after every 4 characters

Here is the sample input: AbbigailAbieAbbyAbbi Using sed, I have separated them into 4 characters but seems like I got a problem because I have to separate the first 8 characters first then add ...
3
votes
2answers
84 views

Extract csv files from path to multiple files in bash

I have a files multiple file path for backup purpose ,now i want to filter it based on ".csv " fromat. I have tried this. filesall=(/home/abc/allfiles/*) files=$((${filesall[@]}|grep -F ".csv")) ...
1
vote
1answer
129 views

Pattern replacement

Please can anyone explain to me in brief the syntax of this command. Explanation in terms of how actually this command is written significance of each and every character used. I know the output of ...
0
votes
1answer
40 views

Quote escaping struggle with grep and sed

I'm writing a bash script to extract data from html tags and I found this command in another thread : grep -o '<tr>.*</tr>' HTMLFILE | sed 's/\(<tr>\|<\/tr>\)//g' > NEWFILE, ...
3
votes
5answers
328 views

How to use a shell variable inside sed's s command? [duplicate]

we know that we can get the value of a variable using $ sign: x=3 echo $x 3 Is there other ways that we can use to get the value without using $ sign. I'm asking this because $ sign is a special ...
7
votes
0answers
133 views

regarding portable sed -e… d b or ! b?

In this edit St├ęphane Chazelas POSIXifies (again) my sed formatting by inserting an -expression break and another -expression statement. Now, I might just ask him why in the comments, I suppose, but ...
5
votes
3answers
215 views

sed ranges aren't always able to match only one line

Consider the following range: 1,/pattern/. If pattern matches on the first line, the range matches for the whole file: $ cat 1.sh #!/usr/bin/env bash set -eu seq 1 4 | sed -rn '1,/'"$1"'/p' $ ./1.sh ...
4
votes
2answers
239 views

sed to search for multiple words and remove those lines

I have a master file with more than 50000 lines and my requirement is to remove around 20000 lines from actual file, for this I used grep -vE command which is showing an error as too many arguments. ...
1
vote
2answers
91 views

List all the html tags in a file

I want to know if there is a way to list all the html tags in a file. Lets say I have a file file.html <html> <head> <title>Test</title> </head> <body> This is a ...
3
votes
1answer
92 views

Is there a way to pass a variable from an awk statement to a bash function as an argument? [closed]

I am trying to look a for some specific values from (such as 1 or 4 and 2 or 3) a text file using an awk statement in a bash script. If this value is found in the file (within the awk statement), then ...
4
votes
3answers
257 views

Regex alternation/or operator (foo|bar) in GNU or BSD Sed

I cannot seem to make it work. GNU sed documentation says to escape the pipe, but that doesn't work, nor does using a straight pipe without the escape. Adding parens makes no difference. $ echo ...
0
votes
3answers
46 views

Finding and replacing text inside awk block

I want to find and replace a value inside awk block. Here is a sample input file to my script P1 10,9:11/18013013582 ,10:1 ,167:0 ,487:5/E.164 11,9:15/310410532169026 ,10:60 ,167:0 ,487:4/IMSI ...
3
votes
6answers
3k views

How to use the sed command to only show the first column and last column in a text file

I need some help to figure out how to use the sed command to only show the first column and last column in a text file. Here is what I have so far for column 1: cat logfile | sed 's/\|/ /'|awk ...
1
vote
2answers
119 views

Compare set of values from two files and print the output in third file in awk

Hi friends i have problem with comparing two files.I'll try my best to explain.I have two file f1 and f2 which i have to compare on basis of two sets Parameter(p) and Value(v). Basically f1 contains ...
1
vote
3answers
44 views

Shell command to remove the first string that is enclosed with slashes

I need a shell command(sed or awk is preferred) to remove the string that is enclosed within the slashes. For example If my string is /some/dir/example/ then the output should be dir/example/
1
vote
1answer
65 views

What can I know about this part of code? [duplicate]

What does this part of a script do? This is a part of a scrpt in bash for j in *.* ; do cp $j ../../$name-S$i.gid/data/${j%%.*}$i.${j#*.} sed "s/$name-S/$name-S$i/" $j > ...
0
votes
1answer
210 views

Escaping multiple backticks in a sed call

I need to perform search and replacement iteratively in several SQL statements: From: CREATE TABLE `Old_Name` ( To: ALTER TABLE `Old_Name` RENAME TO `New_Name` The above query contains ...
0
votes
2answers
102 views

Method to paste specific column when in terminal

I'd like to paste only the third column of data from within the terminal. I originally have something like: 4 SF0086 SF0086 12 SF0087 SF0087 17 TSF141 TSF141 29 TSF070 TSF070 30 TSF028 TSF028 31 ...
3
votes
2answers
1k views

How can I print out all but the last n characters in bash?

I'm working with standard input (cating two files together) and want all but the last 10 characters of the result to be passed through the next command. All I can find through searching is using sed, ...
6
votes
4answers
3k views

How to extract logs between two time stamps

I want to extract all logs between two timestamps. Some lines may not have the timestamp, but I want those lines also. In short, I want every line that falls under two time stamps. My log structure ...
0
votes
1answer
105 views

Commands inside bash not being interpreted?

I'm trying to execute a command inside a bash script, $(pwd) is not being interpreted at all, and not even using eval. This do not work: cat apache-vhost.conf | sed 's/{path}/$(pwd)/g' Neither ...
4
votes
2answers
292 views

Merge fields in a file

I have a file with 7 columns, a GFF file having chromosomal regions.I want to collapse the rows where REGION ="exon" to only one row in the file.The row has to be collapsed on the basis of regions ...
0
votes
2answers
299 views

Using grep to find first two numbers of IP addresses?

I'm trying to figure out how to use grep to pull out only the first two numbers of an IP address in a list. I've got a routine that is isolating the log entires (from Exim) that I need, now I just ...
4
votes
2answers
657 views

Replace string with sequential index

Can someone suggest an elegant way to accomplish this? Input: test instant () test instant () ... test instant () //total 1000 lines output should be: test instant1 () test ...
2
votes
2answers
50 views

join sed output

I'm looking to somehow pass the output of the following command: $ sed -n '5p' file-A into $ sed '5 c\ > "string extracted from previous sed command" file-B So basically, I want to replace ...
1
vote
1answer
217 views

How to get the JSON STRING from the given string

I have a string given below. I just need to get the first accountHeader (bolded) JSON string, I tried everything.. It didn't work. Can you help me to get the data? <START ...
4
votes
2answers
93 views

Why sed prints first string from file even if it's failed to find matching groups

I'm trying to write sed expression which will extract matching groups from file. E.g. I have .cue sheet and I want to extract it's genre tag. It has following format: $ cat file.cue | grep GENRE REM ...
1
vote
2answers
191 views

Escaping blackslash in sed

I am trying to write a sed command to replace a line in a file. The sed replace required the current working directory, which starts making the command a bit messy because of the characters that need ...
1
vote
1answer
125 views

Using sed output in another script or command

How can I use the output of sed in another script? For example (this doesn't work): sed -n "$COUNTER",1p /domains.csv | wget or sed -n "$COUNTER",1p /domains.csv > /myScript.sh As far as I ...
0
votes
3answers
68 views

linux + awk + file manipulation

I have the following file more file.txt 1 2 3 4 4 2 23 4 5 23 2 3 END_OF_LINE 1 3 7 4 2 23 4 6 7 4 2 1 5 How to print only the fields 4 and 5 and 6 until the line END_OF_LINE , and from ...
1
vote
2answers
206 views

What does this regex mean?

I was working on the conversion of Unix newline to Windows newline. I tried to unix2dos but it gave me some binary error, so I looked it up and stumbled upon this regex sed 's/$'"/`echo \\\r`/" ...
6
votes
2answers
390 views

what is the meaning of this sed command: `;/@/{h;s/test/next/g;x;G}`?

sed -e 's/72;/72, next_val = 0x11111111;/;/@/{h;s/test/next/g;x;G}' fmt_vuln.c > fmt_vuln2.c could you tell me about what is the meaning of ;/@/{h;s/test/next/g;x;G}?
8
votes
6answers
699 views

How to get multiple lines out of a file by a regex?

How to get multiple lines out of a file by a regex? I often would like to get multiple lines / modify multiple lines by a regex. A example case: I am trying to read part of an XML/SGML file (they ...
3
votes
2answers
960 views

Replace a pattern in a file with a long string

I'm writing a little shell script to generate a directory listing. To make the output easily customizable, the script just builds a HTML-table and than should replace a specific token in a template ...
13
votes
6answers
690 views

How to achieve portability with sed -i (in-place editing)?

I'm writing shell scripts for my server, which is a shared hosting running FreeBSD. I also want to be able to test them locally, on my PC running Linux. Hence, I'm trying to write them in a portable ...
0
votes
2answers
684 views

How to remove n number of lines from a file using sed command? [duplicate]

I want to remove a particular number of lines after the given pattern is matched. Can anyone please tell me how that can be done using the sed command ?
1
vote
1answer
87 views

What are the commands that you can use in the beginning of a sed command?

I initially learned to do sed search and replace like so: sed -e 's/<stuff>/<replacement>/' However I recently learned that these will do something different based on what command ...
2
votes
2answers
3k views

Passing a variable to sed

I cannot not use a shell variable in sed in the $NUMBER form. I have this line in my shell script: cat shared.txt sed 's/whatever1/$2 ... whatever2/' > shared2.txt The result in shared2.txt ...
1
vote
3answers
158 views

Cat 2 files and avoid duplication of lines

I have the file1 with the following content {"name":"clio5", "value":"13"} {"name":"citroen_c4", "value":"23"} {"name":"citroen_c3", "value":"12"} {"name":"golf4", "value":"16"} {"name":"golf3", ...
-1
votes
1answer
222 views

SED command and filenames with space

I was hacked, for my php files was infected. Example: http://www.nwww.ma.mk/oscommerce/configweb/apexfash-wp-blog.txt Bad Code: <?php ...
2
votes
3answers
2k views

sed - how to replace a single quote?

This works fine: sed -i 's# @driver.find_element(:xpath, "//a\[contains(@href,##' temp_spec.rb against a source of @driver.find_element(:xpath, "//a[contains(@href,'change_district')]").click ...
5
votes
4answers
486 views

sed how to substitute when string has “http://” in it?

I have a file xx that has the following contents: @base_url = "http://dmstaffing-stage.herokuapp.com/" I want to use sed to eliminate this line (replace with nothing). I have used this sed ...
3
votes
4answers
1k views

Replacing pattern after nth match is found on each line?

I have a file containing lines: india;austria;japan;chile china;US;nigeria;mexico;russia I want to replace all the occurences of semicolon on each line with e.g. ;NEW;, but starting from the 2nd ...
1
vote
4answers
84 views

How to find what device a file is on (and use that in a script)?

I want to find out what device my file is on so that I can use it in a script. I can get this far: $ df . Filesystem 512-blocks Used Available Capacity Mounted on /dev/disk0s2 498438976 ...
0
votes
3answers
152 views

How to delete number of lines from file repetitively

I've read How do I delete the first n lines of an ascii file using shell commands?, it is helpful. However I've a file something as below (please consider 2 columns as 2 different files): 1 4 1 4 1 4 ...
1
vote
4answers
1k views

How to rename files with sed and csv

I'm new with Linux, sed, and awk but I don't mind challenging myself on new ideas. That being said, I understand the purpose and how to use rename and sed for a common event such as adding a $date or ...