answerstu

unix - awk: output with lines before and after a match

I've just started using bash and I've asked this question about an issue that I had using the loop 'while' with 'grep': grep issues when using two files - I've tried everything.I was told to use 'awk' instead of 'grep' in looping due to the following issues https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practiceReplacing 'grep' in loop for 'awk' indeed worked. However, I realized that I need an output that saves not only the matched line but also 1 line before and 2 lines after.I goo...Read more

unix - Start systemd service from C/C++ application or call a D-Bus service

I have a .service for a process that i don't want to start at boot-time, but to call it somehow from another already running application, at a given time.The other option would be to put a D-Bus (i'm using glib dbus in my apps ) service file in /usr/share/dbus-1/services and somehow call it from my application. Also, i don't manage to do this either.Let's say that my dbus service file from /usr/share/dbus-1/services is com.callThis.serviceand my main service file from /lib/systemd/system is com.startThis.serviceIf i run a simple introspect from...Read more

unix - Forwarding logs from file to journald

I have an application on the isolated machine. It writes logs to /var/log/app/log.txt for example. However, I want it to write logs to journald daemon. However, I can't change the way application run, because it is encapsulated.I mean I can not do smth like app | systemd-cat1) Am I right that all services started with systemd write logs to journald?2) If so, will the children of process, started by systemd, will also write logs to journald?3) Is there any way to tell journald to take logs from a specific file?4) If not, are there any workaround...Read more

Regarding the Unix command "wc" what is considered as a word?

The command wc provides lineCount, wordCount, and charCount. I am writing a program that simulates the wc command as it takes a file and spits out the 3 properties. Line count is easy because if it sees \n it will ++lineCount and if a char exists and it's not EOF, it will ++charCount. But what does word mean? What separates words, whitespace?...Read more

unix - How to send the output of this command: find ./ -type f -exec grep -H 'String' {} \; to a file?

Somewhere in here someone shared this cool way to find stuff in unix:find ./ -type f -exec grep -H 'string to be searched' {} \;And I was wondering how to send the output of it to a file.I tried the basic >> something.log but it seems to ignore it, perhaps because of the last part of the command {} \;Could you help me to achieve this?Thank you in advance....Read more

unix - Use grep --exclude/--include syntax to not grep through certain files

I'm looking for the string foo= in text files in a directory tree. It's on a common Linux machine, I have bash shell:grep -ircl "foo=" *In the directories are also many binary files which match "foo=". As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPEG and PNG images). How would I do that?I know there are the --exclude=PATTERN and --include=PATTERN options, but what is the pattern format? The man page of grep says:--include=PATTERN Recurse in directories only searching file mat...Read more

unix - What are the dark corners of Vim your mom never told you about?

There are a plethora of questions where people talk about common tricks, notably "Vim+ctags tips and tricks".However, I don't refer to commonly used shortcuts that someone new to Vim would find cool. I am talking about a seasoned Unix user (be they a developer, administrator, both, etc.), who thinks they know something 99% of us never heard or dreamed about. Something that not only makes their work easier, but also is COOL and hackish. After all, Vim resides in the most dark-corner-rich OS in the world, thus it should have intricacies that onl...Read more

unix - Monitoring Linux server processes

This is for some suggestion and choice of right approach to monitor.There is a RHEL6 server running several processes running under several SILOs and sandboxes, we need to monitor these process making sure these processes are up and running.Currently I have made a script to which loops through all the sandboxes and check for the process status, then I send out a mail to the respective team with the list of jobs not running, as attachment.Problem: Now since the number of sandboxes and jobs are quite many, it takes time to check and process and...Read more

unix - Responsive lightweight remote GUI connection

Currently I know several methods of connecting to a GUI remotely, or running GUI applications remotely:Microsoft Terminal Services (onlyworks for remote windows;installation);VNC (it's slow);XDMCP (requires a remote X serverrunning, has no session persistance);Local X as remote DISPLAY forapplications (best solution, but -no session persistance).We are trying to create Solaris development environments that can replace local workstations for our developers. So one of the requirements is session persistence, and/or session mobility. And another r...Read more

unix - Extract rows if max-min in that row fulfill certain conditions

I have a tab-delimited file with the lines as:chr1 1001 + KEEP 0.5 0.3 0.06 0.4 0.2 0.3 0.5chr1 40004 + KEEP 0.93 0.2 0.6 0.25 0.8 NA NAchr2 140004 + KEEP 0.03 0.02 0.06 0.05 0.08 NA 0.01chr2 455235504 - KEEP NA 0.12 0.67 0.51 0.8 NA NAchr3 5004 + KEEP 0.53 0.52 NA 0.5 NA 0.54 NA..I want to calculate the difference between the max and min on each line from the 5th column onwards, and extract the rows in which this (max-min) difference is equal or more than 0.1. So for example with the input given above, I should get:chr1 1001 + KEEP 0.5 0.3 0.0...Read more

How can I extract a predetermined range of lines from a text file on Unix?

I have a ~23000 line SQL dump containing several databases worth of data. I need to extract a certain section of this file (i.e. the data for a single database) and place it in a new file. I know both the start and end line numbers of the data that I want.Does anyone know a Unix command (or series of commands) to extract all lines from a file between say line 16224 and 16482 and then redirect them into a new file?...Read more

SFTP to a legacy server using UNIX

I have a requirement to connect to a different server using SFTP with username and password for sending files from Oracle Server to a legacy server. How do I achieve this using a UNIX Shell script(KSH)? Please help!Thanks,Arun...Read more