Wildcard ".*.a" on Unix matches what? [closed] - unix

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.

Related

How can I convert double slashes in R without encountering error relating to escape characters? [closed]

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.

csv file, or tab delimited file, or whatever file [closed]

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 3 years ago.
Improve this question
I have a question regarding file.
The reason I am asking is because of using some function for example in R to import the data from outside to R.
I have a file character_student.txt with data like below.
Name, Age, Gender, Test1
john, 11, M, 90
betty, 25, F, 33
I am confused. Is the above file considered as csv file (comma separated file)? Or is it a text file? When using R to import say this file to R, is it appropriate to use say read.csv(file="character_student.txt)?
Then the other question that I have is, if I have a file like this:
Name Age Gender Test1
john 11 M 90
betty 25 F 33
so there is only a single space in between each file, and then say I saved it as a .csv file, then I think the filename will become something like character_text.csv. Then I am just wondering is this file now a space-delimited file or a comma-delimited file?
I guess my question is how do I know if I file is a comma-separated file? or a space-delimited file? or a tab-delimited?
Is it purely based on the name of the file? for example if the name ends with csv, then it is comma separated file, if it ends with something else then is "something else" delimited file? so does it matter what the inside of the file actually looks like? like do we have to open the file to check if there is a comma separating the field to be sure that the file is a comma-separated file? or if we have a csv file but inside of it, the field could be separated by something else?
Or if it is called csv, then every field inside is separated by a comma (like I don't have to open it to make sure that it is actually separated by comma)?
Extensions don’t define files. They help various utilities or tools to process them in a specified way.
You write a python script and save it as hello.c.
You then pass it to gcc like gcc hello.c.
Nothing is wrong with that. gcc will accept to process the file but report lots of syntactical errors.
Similarly, by specifying .csv, you are telling the tool, utility or function that you are passing a comma separated file.
If you have a file like:
abc def, ghi jkl,
One user wants to extract data from it in the form:
abc, def,, ghi and jkl,. For that user it would be good if he “treats" it like a space separated file. For some other user who wants,
abc def and ghi jkl, it would be useful for him to treat it as a comma separated file.
For a particular case, you need to study that particular function or tool and analyse the way they need the file. So yes, if a tool wants a file to be in a particular way, you need to make checks to pass the file to that tool accordingly.
Its just about how you want it.

Unix Replace specific column with one value for another string [closed]

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

unix:find files that start with capital

Basically,i want to copy files from one directory to another.The condition is that the file must not have as first letter a capital and must end in (.txt).
(This is my first question in stackoverflow so be gentle )
This do the trick cp [a-z]*.txt directory

In R, How to remove some unwanted charaters from the CSV file names and also extract dates? [closed]

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 7 years ago.
Improve this question
I have a folder which contains some 2000 CSVs with file names that contain character '[ ]' in it - e.g.: [Residential]20151001_0000_1.csv
I want to:
Remove '[]' from names so that we have file name as:
Residential_20151001_0000_1.csv
and place new files within a new folder.
The read all the files from that new folder in one data frame (without header) after skipping first row from each file.
Also extract 20151001 as date (e.g. 2015-10-01) in a new vector as list such that the new vector is:
File Name Date
Residential_20151001_0000_1.csv 2015-10-01
This code will answer your first question albeit with a small change in logic.
Firstly, lets create a backup of all the csv containing [] by copying them to another folder. For eg - If your csvs were in directory "/Users/xxxx/Desktop/Sub", we will copy them in the folder Backup.
Therefore,
library(stringr)
library(tools)
setwd("/Users/xxxx/Desktop/Sub")
dir.create("Backup")
files<-data.frame(file=list.files(path=".", pattern = "*.csv"))
for (f in files)
file.copy(from= file.path("/Users/xxxx/Desktop/Sub", files$file), to= "/Users/xxxx/Desktop/Sub/Backup")
This has now copied all the csv files to folder Backup.
Now lets rename the files in your original working directory by removing the "[]".
I have taken a slightly longer route by creating a dataframe with the old names and new names to make things easier for you.
Name<-file_path_sans_ext(files$file)
files<-cbind(files, Name)
files$Name<-gsub("\\[", "",files$Name)
files$Name<-gsub("\\]", "_",files$Name)
files$Name<-paste(files$Name,".csv",sep="")
This dataframe looks like:
files
file Name
1 [Residential]20150928_0000_4.csv Residential_20150928_0000_4.csv
2 [Residential]20151001_0000_1.csv Residential_20151001_0000_1.csv
3 [Residential]20151101_0000_3.csv Residential_20151101_0000_3.csv
4 [Residential]20151121_0000_2.csv Residential_20151121_0000_2.csv
5 [Residential]20151231_0000_5.csv Residential_20151231_0000_5.csv
Now lets rename the files to remove the "[]". The idea here is to replace file with Name:
for ( f in files$file)
file.rename(from=file.path("/Users/xxxx/Desktop/Sub", files$file),
to=file.path("/Users/xxxx/Desktop/Sub",files$Name))
You've renamed your files now. If you run: list.files(path=".", pattern = "*.csv") You will get the new files:
"Residential_20150928_0000_4.csv"
"Residential_20151001_0000_1.csv"
"Residential_20151101_0000_3.csv"
"Residential_20151121_0000_2.csv"
"Residential_20151231_0000_5.csv"
Try it!
In order:
After googling r replace part of string I found: R - how to replace parts of variable strings within data frame. This should get you up and running for this issue.
For skipping the first line, read the documentation of read.csv. There you will find the skip argument.
Have a look at the strftime/strptime functions. Alternatively, have a look at lubridate.

Resources