Tag Info

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/


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

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

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


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

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


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

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


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 ...


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 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

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

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

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


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

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



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