LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Blogs
User Name
Password

Notices


Old

BASH extract fields from string

Posted 04-05-2020 at 05:19 PM by karanis
Tags awk, bash

https://en.wikibooks.org/wiki/Cut

link above have nice examples

Examples of use of similar tools in cut's domain:

echo a b|awk '{print $2}'
Displays "b", separating fields by any number of tabs or spaces. Thus, for awk, "b" is the 2nd field, while for cut -d" ", "b" is the 3rd field.
echo a b|awk '{print $2 $1}'
Displays "ba", preserving the order of the fields as specified....
LQ Newbie
Posted in Uncategorized
Views 366 Comments 0 karanis is offline
Old

BASH read file

Posted 04-05-2020 at 05:17 PM by karanis
Tags bash

https://www.cyberciti.biz/faq/unix-h...ine-from-file/

https://www.computerhope.com/unix/bash/read.htm

https://www.shellhacks.com/bash-read...ead-line-loop/

----------

while read USER; do echo "Hello $USER!"; done < users.txt

----------

#!/bin/ksh
file="/home/vivek/data.txt"
while IFS= read line
do
# display $line...
LQ Newbie
Posted in Uncategorized
Views 305 Comments 0 karanis is offline
Old

BASH datetime for scripts

Posted 04-05-2020 at 05:16 PM by karanis
Tags bash, time

#!/bin/bash
dt=`date '+%d/%m/%Y %H:%M:%S'`
echo "$dt"
LQ Newbie
Posted in Uncategorized
Views 293 Comments 0 karanis is offline
Old

BASH get ip from ifconfig ( and grep hostname from /etc/hosts by IP)

Posted 04-05-2020 at 05:13 PM by karanis
Updated 04-05-2020 at 05:15 PM by karanis
Tags bash, net

#!/bin/bash

IP=`/usr/sbin/ip addr show dev eth0| grep -v inet6| awk ' !/127.0.0.1/ && /inet/ { gsub(/\/.*/, "", $2); print $2 }'`
grep $IP /etc/hosts | sed -r s/$IP\\s\\t//
LQ Newbie
Posted in Uncategorized
Views 775 Comments 0 karanis is offline
Old

BASH delete line from file

Posted 04-05-2020 at 05:12 PM by karanis
Tags bash

https://www.folkstalk.com/2013/03/se...-examples.html

sed '/^u/d' file
Deletes a line starting with u
LQ Newbie
Posted in Uncategorized
Views 179 Comments 0 karanis is offline

  



All times are GMT -5. The time now is 05:49 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration