Manipulation of text by programs, scripts, etc.

learn more… | top users | synonyms

0
votes
2answers
16 views

add lines for each 3 lines

I have a document like this 1 0.2 1 0.3 1 0.1 2 0.8 2 0.5 2 0.3 I want to add the lines if the first column are the same and output something like this 1 0.6 2 1.6 How can I do this with awk? Or ...
8
votes
2answers
437 views

Binary search in a sorted text file

I have a big sorted file with billions of lines of variable lengths. Given a new line I would like to know which byte number it would get if it had been included in the sorted file. Example a\n c\n ...
6
votes
4answers
331 views

Merge lines into single line

I have a file like this: < Dec 2, 2015 2:51:49 PM EST> <Error> <HTTP> <cphypprod1v..com> <AnalyticProviderServices0> <[ACTIVE] ExecuteThread: '3' for queue: '...
1
vote
3answers
49 views

Extract lines from a large file that do not end with “H” into another file

I have a file that looks like the following: TITLE Protein in water t= 0.00000 REMARK THIS IS A SIMULATION BOX ATOM 1 N SER A 107 20.799 63.728 25.985 1.00 0.00 N ...
5
votes
5answers
111 views

Non-greedy match with SED regex (emulate perl's .*?)

I want to use sed to replace anything in a string between the first AB and the first occurrence of AC (inclusive) with XXX. For example, I have this string (this string is for a test only): ...
0
votes
1answer
32 views

Altering Loop count

I have multiple code files with lot of loops. Now I have to change all the loop counts to 4 times the current count in all the files. Is there a way I can achieve this for all the files simultaneously....
7
votes
2answers
132 views

Make GNU indent break long comments

Is there a way to convince GNU indent to break long comment and change it to multiline comment? Something like this: // Very long comment, longer than 80 characters. Just imagine that. To this: /* ...
0
votes
2answers
74 views

Split file by pattern

Is there an easy way to split, for example, apache vhost file with multiple vhost into 1 vhost per file? Or something else, allowing to operate only one by one vhost to get grep output . preferred ...
0
votes
1answer
35 views

how to update the values in config file

I have an Config.yml file i want to update the values like below by using shell script. current file data servers: - uri: http://localhost:5550/service/mgmt/current - displayName: server1 - ...
0
votes
3answers
97 views

Compare a file with a column of another file

I have a file File1 with data as aaa bbb And another file File2 with data as: 2,aaa,234 w,bbb,589 4,ccc,675 I need to compare the File1 data with column2 data of File2 and print the matching ...
0
votes
0answers
32 views

Output text between two strings [duplicate]

I have nmap output that is in a format similar to the following: Nmap scan report for 192.168.2.1 Host is up, received user-set (0.0055s latency). 23/tcp closed telnet reset ttl 55 Nmap ...
1
vote
2answers
40 views

Merging specific lines in 3 files by specific pattern

I have 3 file like these: file1: ko00980 Metabolism of xenobiotics by cytochrome P450 (5) ko:K00121 frmA; S-(hydroxymethyl)glutathione dehydrogenase / alcohol dehydrogenase [EC:1.1.1.284 1.1.1.1] ...
1
vote
2answers
172 views

Replace only first instance of a character

for example I have scv file which looks like a1, b1, c1, d1 a2, b2, c2, d2 a3, b3, c3, d3 What I want to do is to replace the first comma , with the semicolon ;. The position of first comma can be ...
1
vote
3answers
51 views

Appending a character in the nth position of a matching string

I need to find a sed/awk command which will insert a character at the nth position of a matching string HAVING LENGTH 10. For example, I need to search for the string starting with pattern 541 and ...
8
votes
6answers
982 views

How can I delete all characters falling under /* … */ including /* & */?

I did tried sed and awk, but its not working as the character involves "/" which is already there in command as delimiter. Please let me know how can I achieve this. Below is a sample Example.We ...
2
votes
2answers
40 views

Merging 2 files records by template

I am trying to use awk to merge two text files in a rather peculiar way, taking two lines from file1, a group of word(s) from file2 (but placed on a separate line), alternating ad infinitum. Groups ...
4
votes
2answers
3k views

grep one line before the match plus the match

zzzzzzzzz aaaaaaaaa bbbbbbbbb & ccccccccc & ddddddddd hhhhhhhhh eeeeeeeee fffffffff & ggggggggg & in the above line, what i want is to grep/sed/awk (any method is fine) line that ...
1
vote
2answers
39 views

Returning the output from Perl Regex

I've been trying to just return the inet values from UP adapters in ip addr, 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:...
2
votes
4answers
39 views

Merge lines between keywords into one-line comma separated values

Between first occurrence of Cat to next occurrence of Cat, it should create a separate line with delimiter as ",". File input as below. Cat AA BB CC Cat AA-1 BB-1 CC-1 Output expected: Cat,AA,BB,...
1
vote
2answers
96 views

Modify specific field text inside double quotations

I need help modifying everything inside of specific double quotation marks. Example of my work is: <VALUE MAP_ID="1001" MAP="0" MAPNAME="BichonTown" SERVER="0" CHNSERVER="0" NEEDLEVEL="7" TIME="0" ...
1
vote
2answers
46 views

How to insert a string between 2 consecutive ,?

I want to insert a string possibly N.A. between two consecutive , This is my string. 5 Local A2A SMS valid for 1 day , 10 A2A SMS valid for 2 days , 10 Local A2A SMS for 2 days only , Daily 5 Local ...
314
votes
5answers
121k views

Why is printf better than echo?

I have heard that printf is better than echo. I can recall only one instance from my experience where I had to use printf because echo didn't work for feeding some text into some program on RHEL 5.8 ...
1
vote
1answer
31 views

Replace the values of some keys in a single line text file

I have a large file called res.dat that contains certain data I need modified. All data is on one single line. For example: L1TRAIN="200" NEEDL2="14" L2TRAIN="300" NEEDL3="16" L3TRAIN="400" NEEDL4="...
0
votes
2answers
37 views

Extract the value part from key-value pair separated by colon in Linux

I need to fetch arn:aws:sns:us-west-2:596873354795:demo from the below content in a file using sed command. What would be the command? Sample file: { "TopicArn": "arn:aws:sns:us-west-2:...
0
votes
3answers
50 views

How can I delete a line from a file when the line contains a specific string? [duplicate]

I have a file which is comma separated. I have to find specific text and then delete complete line. e.g. 123,aaa,bbb,888,8881 124,bbb,ccc,999,8881 125,ccc,ddd,888,8883 I have to delete the line ...
1
vote
2answers
63 views

Finding the last character of a file and adding a string after that? [duplicate]

Appending an extra line is simple as I can use echo "line" >> file but what if I want to add a string right after the last char in the file without starting a new line? What are some good ways ...
0
votes
1answer
23 views

Remove last n characters from -print0?

Currently I have: @find . -type f -name "img*_01.png" -print0 | python script.py -f {} is there a way to trim the last few chars like this? @find . -type f -name "img*_01.png" -print0 | python ...
1
vote
1answer
15 views

Splitting a file into multiple files based on 1st column value [duplicate]

I have seen solutions of splitting a file with respect to pattern matching and line matching but what I want is the following. The scenario is, let's say I have a file file1 - A.B|100|20 A.B|101|...
3
votes
2answers
48 views

how to get content within a specific text from a file

I have a file name demo.txt with content as follows: value -= [ "02|05|06|abc", ] /* Some other content other than value variable */ value -= [ ] value -= [ "0698|06|07|abc", ] I have lots of ...
7
votes
3answers
593 views

Replace all occurences of two spaces after the end of a sentence with just one space

I'm stuck in including regular expressions with a sed command. Q: I want to replace all occurences of two spaces after the end of a sentence with just once space. Here is what I did: sed 's/^ ...
1
vote
4answers
82 views

Merge two files: two lines, partial line, two lines, partial line, etc [closed]

Hello I am trying to use awk to merge two text files in a rather peculiar way, taking two lines from file1, a group of word(s) from file2 (but placed on a separate line), alternating ad infinitum.  ...
2
votes
2answers
64 views

Text Manipulation: Removing values x <= $input from sorted list & generalizing

My input text file contains a 1 line header, then a sorted list of lines containing: x y with x,y 64-bit integers separated by a space. The Input file is many million lines long. Now I want to ...
1
vote
1answer
43 views

Problem with echo command

I want read a file and after if there is one "KO" in the file ill break the program and print "KO". If there are all "OK" i want that at the end of the program it will display me "OK". Here the code: ...
4
votes
2answers
213 views

Replace the first occurence of a pattern in a file that may contain a slash

Thanks to http://stackoverflow.com/questions/27787536/how-to-pass-a-variable-containing-slashes-to-sed I know how to pass a variable that contains slashes as a pattern to sed : sed "s~$var~replace~g"...
2
votes
1answer
46 views

Cut string from command output

I would like to cut 150.8 from this string temp1: +150.8°F (crit = +197.6°F). Here is my script for logging temperatures with the command sensors: #!/bin/bash now=$(date +"%m_%d_%Y") # ...
14
votes
4answers
1k views

/usr/bin/ptx: Can you provide a use case or two?

I was going through the list of files included in coreutils and I was able to come up with an example of how I could personally use all of the commands provided except for ptx. Can you give one or two ...
21
votes
5answers
9k views

command to layout tab separated list nicely

Sometimes, I'm getting as an input tab separated list, which is not quite aligned, for instance var1 var2 var3 var_with_long_name_which_ruins_alignment var2 var3 Is there an easy way to render ...
4
votes
1answer
447 views

Sed command that would ignore any commented match

I'm trying to create a sed command using regex in order to substitute something in a text file only if it is not commented but I'm running into some troubles due to my almost non-existent knowledge of ...
1
vote
1answer
29 views

Problem with reading records from a file

I have the following problem: Script #!/usr/bin/ksh for unitcaseno in `cat /pims/nigel/UNLOAD-DB/xaa` do echo "String = $unitcaseno" unitno=`echo $unitcaseno | cut -d'|' -f1` ...
80
votes
3answers
9k views

Why is using a shell loop to process text considered bad practice?

Is using a while loop to process text generally considered bad practice in POSIX shells? As Stéphane Chazelas pointed out, some of the reasons for not using shell loop are conceptual, reliability, ...
3
votes
2answers
32 views

Problem with reading text file encoded in Western encoding (ISO-8859-1)

I'm having a problem with encoding of ISO-8859-1 text file (subtitles in Polish language), which looks something like that: Mieszka³ sam,|¿adnej ¿ony, dzieci. It should be : "Mieszkał sam, żadnej ...
4
votes
1answer
60 views

Splitting file content into other files based on parsed parameter value in file [duplicate]

A regular file contains data organized in columns, separated by space. Based on the value of the second field, I want to split that initial data file into several other files. The data file has 54,...
0
votes
2answers
2k views

Sum two columns from different files

I have two files like this: File1: -54.005 -67.405 0.1 -54.05 -68.7833 0.2 -54.05 -68.7333 0.3 File2: -54.005 -67.4050 70 12.7 -54.05 -68.7833 167 12.5 -54.05 -68.7333 205 12.6 ...
3
votes
5answers
113 views

How to repeat a block of text?

How do I use awk to repeat a block of text 100 times? ie string1 string2 string3 Would become: string1 string2 string3 string1 string2 string3 string1 string2 string3...
3
votes
2answers
81 views

Compare two files and print matching values without ignoring duplicates

I am trying to comparing two files and obtain the matching values. I tried this command: grep -Fwf file_1.txt file_2.txt > matched_output.txt However, this script extracts only unique values. ...
1
vote
2answers
59 views

Reformatting Apache RewriteRule

I have these variant of urls RewriteRule ^/trendy/the-reason-for-example/? http://www.example.com/trendy/the-reason [NC, L, R=301] RewriteRule ^/lol/2015/10..._for-example http://www.example.com/lol/...
4
votes
4answers
598 views

Parse a txt file based on a specific delimeter and then tranform it into a CSV file

I have the following file named OpenSimStats.txt: TestreportsRootAgentCount=0agent(s) TestreportsChildAgentCount=0childagent(s) TestreportsGCReportedMemory=10MB(Global) TestreportsTotalObjectsCount=...
3
votes
2answers
227 views

How to add text to the start of a line?

I have a text file and I want to prepend some text to the first line. I tried something like this: sed -i '1i\'"string" file However this inserts a new line to the text file.
17
votes
4answers
32k views

How to insert text before the first line of a file?

I've been looking around sed command to add text into a file in a specific line. This works adding text after line 1: sed '1 a\ But I want to add it before line 1. It would be: sed '0 a\ but I ...
1
vote
5answers
553 views

Check number of words in a file that contain a specific letter

Bash command to check number of words in a file that contain letter “a”