Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I would like to change the fourth column of a file when I have the following description for it. I can not find how to solve it. When the string 1-8 appears I want to replace it with 01-08 but only in the 4 column separated by pipe.
SBMM01|CAM|22|01-08|NAP|VL|OPEN|1
CCSM01|CAM||1-8|NAP|CR|CLOSED|1
EZEM01|CAM|19|01-08|SPL|CC|OPEN|5
SPTD01|CAM|29|25-32|CDO|VG|OPEN|1
NRFL01|||1-8|NAP|CR|CLOSED|5
|||1-8|NAP|CR|CLOSED|5
by
SBMM01|CAM|22|01-08|NAP|VL|OPEN|1
CCSM01|CAM||01-08|NAP|CR|CLOSED|1
EZEM01|CAM|19|01-08|SPL|CC|OPEN|5
SPTD01|CAM|29|25-32|CDO|VG|OPEN|1
NRFL01|||01-08|NAP|CR|CLOSED|5
|||01-08|NAP|CR|CLOSED|5
How can change it with sed or awk in Unix?
to change only the specified value
$ awk 'BEGIN{FS=OFS="|"} $4=="1-8"{$4="01-08"}1' file
you can more generally format printing digits zero padding as well.
With sed
sed -E 's/((.*|){3})(1-8)(.*)/\101-08\4/' infile
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I am trying to set my working directory within R to a specific location on my windows computer. However, the destination that I wish to set the working directory to has a somewhat irregular pathway. That is, it begins with two double slashes, and then only single slashes are used through the rest of the path. Because of this, I get error: unrecognized escape character.
To fix this, I have tried a few common R packages along with using raw strings. The functions/packages I have tried so far are gsub, readLines and enc2vec.
However, none of these resolve the issue, and I always end up with double slashes where I need single slashes.
Below, I will provide an example that helps illustrate my issue;
For example, the path that I need to set my working directory to has the form
\\Z\image\pic\store
But,if I try the basic path="\\Z\image\pic\store" , I get the escape character error.
What can I do?
Edit: It appears is it just an issue with how windows interprets the slashes, as one user pointed out, using the cat function will show how it is interpreted. Hence I fixed the issue by using quadruple slashes in place of double slash, and double slash in front of single slashes.
If \\Z\image\pic\store is a valid path then we can refer to it in R using
path <- r"{\\Z\image\pic\store}"
# test
cat(path, "\n")
## \\Z\image\pic\store
See ?Quotes for some variations of this syntax that are possible as well.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have the string I got from html_text()
feel sore — болеть feel sore about — страдать; мучиться
But it should be like this
feel sore — болеть feelsore about — страдать; мучиться
The problem is, rvest doesn't distinguish whitespaces from line breaks, but I need to get only the first line " feel sore — болеть" somehow.
I tried using stringr::str_extract() but failed. What do I do?
UPD: ok I've found out there's html_text2() but is it still possible to use regex?
You can use two negative character classes:
[^—]+: this matches any character that is not —one or more times
[^A-Za-z]+: this matches any character that is not an upper- or lower case letter of the English alphabet one or more times:
Data:
str <- c("feel sore — болеть feel sore about — страдать; мучиться",
"so long — разг. Пока!")
Solution:
str_extract_all(str, "[^—]+—[^A-Za-z]+")
[[1]]
[1] "feel sore — болеть " "feel sore about — страдать; мучиться"
[[2]]
[1] "so long — разг. Пока!"
To get rid of the list character, use unlist; to get rid of the trailing whitespace, use trimws.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to print all the lines of a file in which second and second last character are the same, such as with the following file (comments to the right are for explanatory purposes, they do not exist in the file):
hello james # second/second-last are 'ee' - match
how are you? # 'ou'
are you okay? # 'ry'
Is it past # 'ss' - match
Then the output should be
hello james
Is it past
How would I go about doing this?
You can use grep with grouping and backreference for this, e.g.:
grep -x ".\(.\).*\1." f1.txt
This pattern looks in given order for:
any character: .
another arbitrary character in a capture group: \(.\)
any number (including 0) of characters: .*
the same character previously captured (the backreference): \1
finally, the last arbitrary character: .
-x means it has to match the whole line rather than just some portion of it (same as using --line-regexp). As a result only the matched lines will be printed.
Here is an awk that compare second first and second last character:
awk '{b=split($0,a,"")} a[2]==a[b-1]' file
hello james
Is it past
If there are spaces or tabs at the end of the line, it can be trimmed away like this:
awk '{$1=$1;b=split($0,a,"")} a[2]==a[b-1]'
hello james
Is it past
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
If I execute the command
mv .*.a dir/
what exactly will be moved where? How far would this recurse up?
This moves all files in the current directory that match .*.a into dir. There's no recursion, except that directories matching the pattern are also moved in their entirety (but that's not recursion, because a directory is just a special type of file that can be moved in one operation).
The glob .*.a matches any file or directory whose name begins with a dot (.), then zero or more characters (*), and ends in another dot and an a. So for example, it will match any of
.foo.a
..a
But not
.foo.abar
foo.a
.foo.b
So, any files or directories matching that glob will be moved to he directory dir. No recursion will happen but if the glob matches directories, their contents will also be moved.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a fixed width file ,I want to add a particular character to a specific position of the file for each record.Fixed with file means my columns are not separated with any delimiter.Each column have a specific width assigned to it.
Suppose the below file which I have:
129876 Amit h-no-344 Gurgaon
687 Rahul h-no-2798 Bangalore
5789 Soumyajith-no-33467 Hyderabad
I want to add two blank character (for ex :" ") at the 19th position,so that the length of each record now will be increased by 2.
sed -r 's/.{18}/& /' file
You can use this:
$ awk 'BEGIN{FS=OFS=""} {$19=" "$19} 1' file
129876 Amit h-no-344 Gurgaon
687 Rahul h-no-2798 Bangalore
5789 Soumyajit h-no-33467 Hyderabad
By setting BEGIN{FS=OFS=""}, every nth field is the nth character. Hence, the 19th canbe replaced with {$19=" "$19}. Then the 1 is evaluated as true so {print $0} is executed, printing the line.