Hot answers tagged

6

A good starting point would be: cut -f 3,7 input.txt > output.txt If the file isn't tab delimited, you'll need to add a -d switch with the delimiter ( -d. would be a . delimited file). Here are some examples, including grabbing multiple fields: http://linux.101hacks.com/linux-commands/cut-command-examples/


4

Maybe awk is better suited for this usage: awk 'BEGIN { FS = "|" } ; { print $3 }' If you have to extract more than one field from such an input, I think it is the easiest using awk. (OFF: excuse me if I pointed in an awk-ward direction)


3

You can certainly do this with sed but I know perl better ... perl -p -i -e 's/^(.{12})/$1 /' $INFILE Later sed -i -e 's/^.\{12\}/& /' $INFILE


3

I just figured it out. The shorter username eeeeeee means there's an extra space before the date field. Since the field separator for sort is a non-blank to blank transition, the date field for the line with the shorter username has that space as part of the key field, and gets sorted first. Simple fix: git blame file | sort -b -k 3


3

Why grep? Use cut echo "3|100|test@test.com|0|0|6:1,10,11,12,13,2,3,4,5,6,9|7:1,10,11,13,16,2,4,5,6,9|" | cut -d '|' -f 3


3

Let the read command together with the shell IFS variable parse the line for you: while IFS=: read -r login restOfLine; do doSomethingWith $login done < /etc/passwd To answer your question, the bash here-string would be useful: login=$(cut -d: -f1 <<< "$line")


3

The reason that it doesn't work is because stty is executed within a pipe. Therefore it doesn't "see" the underlying terminal. In your script you could store the terminal width in a variable like size=`stty size | cut -d" " -f2` and then use that next: tail $FILE | cut -c -$size


2

What exactly does not work for you with grep? Try looking at cut command. You are looking at something like: grep YourFileNameHere -e "| Name |" | cut -d " " -f 4 this should parse YourFileNameHere and look for the line containing | Name | then pipe that line to the cut command that will pick the 4th token between (space) delimiters, which according ...


2

Use echo: login=$(echo "$line" | cut -d : -f 1)


2

You are missing a space in your cut command. cut -d\ -f1 should be cut -d\ -f1 since you are using \ to escape the following space (using space as the delimiter), and arguments are separated by a space, there is a missing space between the -d and -f (for cut it looks like you are trying to use " -f1" as a delimiter, which is more than one character).


1

You can use: awk '/%idle/ {for (i=1;i<=NF;i++) {if ($i=="%idle") col=i-1}} /^Average:/ {print $col}' This stores the column where %idle is and substracts one because Average: does not have the PM/AM column. Then, it prints that column when the line starts with Average:. Test With sample1: $ awk '/%idle/ {for (i=1;i<=NF;i++) {if ($i=="%idle") ...


1

You need to use the -s option: $ cut -d ' ' -sf 2- <<< "example" $ By default, if a line has no delimiters, cut will print the entire line. -s turns this behavior off. Not that your second example, does have a delimiter: cut -d ' ' -f 2- <<< "example " Because the line has a delimiter, cut does what you expect. Documentation This ...


1

If you can install python, and easy_install, then you can also install csvkit: https://csvkit.readthedocs.io And, you can now run a simple command like the following to select only columns 1 and 3: csvcut -c 1,3 original_file.csv > new_file.csv Or, another example, to REMOVE the second column: csvcut -C 2 original_file.csv > new_file.csv .. ...


1

This so called rubbish output is basically a progress meter during operation of downloading the data. You can basically ignore that, since it is by default going into standard error stream which is ignored, so only relevant part is printed out to standard output. Here is the test: $ curl http://example.com/ | head -n1 > example.html % Total % ...


1

http://www.videoredo.com/en/index.htm I use it all the time to edit my vidoes


1

on mac os x terminal test="$(echo '1\2\2016' | cut -d '\' -f3-)" echo "year:$test" prints year from test with an echo string year:2016 or echo "year:"$(echo '1\2\2016' | cut -d '\' -f3-)"" prints year from test with an echo string year:2016 try this (similar to above) calendar_date="$(cut -d '\' -f1 $line)" hr_of_day="$(cut -d '\' -f2 $line)" echo "...


1

cut understands the $line argument as a file name. If your shell is bash, you can use the <<< here-word: cut -d' ' -f1 <<< "$line" But, there's no need to call external commands, bash can do it with parameter substitution: date=${line%|*} # Delete from | to the right. hour=${line#*|} # Delete up to |.


1

try this out: awk '\Modelname\ {print}' directory/of/the/file


1

First, on line three, you're trying to run a command stored in the $time variable. You need to echo it to pass it into cut. Second, cut takes a single delimiter, the quotes don't need to be escaped. Try this: setenv time `date | cut -d ' ' -f 4` echo $time setenv hour `echo $time | cut -d ':' -f 1` echo $hour


1

You can specify a range of fields. To get from field 3 to the end: cut -d: -f3- $ line=':user!~name@host.name PRIVMSG #channel :this is the message: all of it' $ echo "$line" | cut -d: -f3 this is the message $ echo "$line" | cut -d: -f3- this is the message: all of it


1

Bash maintains the screen width in the COLUMNS variable, which you can use in a pipeline: tail $FILE | cut -c -$COLUMNS


1

The real problem is that you are trying to parse the output of ls. This is never a good idea, and as you observed, may break easily. For once, it's not even portable across operating systems. Parsing ls is often a symptom of needing to do something else but working around it (and that's an XY Problem). Anyway, you're asking: I am trying to isolate the ...


1

You don't actually need the while loop if your intention is only to list the names. Also there is a syntax error after login=, there should be no space. cut -d: -f1 /etc/passwd | \ while read login; do echo username: $login; done or as you tried: while read line; do login=$(echo $line | cut -d : -f 1) echo $login done < /etc/passwd ...


1

Just for fun, here's how you could do it with grep and tr: <infile grep -Eo '^([^|]+\|){3}' | grep -Eo '[^|]+\|$' | tr -d '|' The first regex grabs the first three pipe delimited fields. The second grep picks out the last field and tr removes the remaining delimiter.


1

Just Imagine your content is present under this file file1 [max@localhost ~]$ cat file1 3|100|test@test.com|0|0|6:1,10,11,12,13,2,3,4,5,6,9|7:1,10,11,13,16,2,4,5,6,9| To cut the third field use this command [max@localhost ~]$ cut -d "|" -f3 file1 test@test.com Here -d : Specifies to use character | as delimiter -f1 : Print first ...


1

It's not clear why you need to make all those directories using mkdir. The only directory you should need to create is the destination directory itself, and that's only if it doesn't already exist. rsync will create all the subdirs if you use the -a or -v option. Something like this should be sufficient: destdir="/home/local_directories/onedir.bak" # or ...



Only top voted, non community-wiki answers of a minimum length are eligible